Bitwise operations: Difference between revisions
Content deleted Content added
Added Algol W |
→{{header|Haskell}}: Pruned out a little noise, added output |
||
Line 1,448: | Line 1,448: | ||
The operations in ''Data.Bits'' work on ''Int'', ''Integer'', and any of the sized integer and word types. |
The operations in ''Data.Bits'' work on ''Int'', ''Integer'', and any of the sized integer and word types. |
||
<lang haskell>import Data.Bits |
<lang haskell>import Data.Bits |
||
bitwise :: Int -> Int -> IO () |
bitwise :: Int -> Int -> IO () |
||
bitwise a b = |
bitwise a b = |
||
mapM_ |
|||
print $ a .&. b |
|||
print |
|||
[ a .&. b |
|||
, a .|. b |
|||
, a `xor` b |
|||
⚫ | |||
, complement a |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
, shift a b -- You can also use the "unified" shift function; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
, rotate a b -- You can also use the "unified" rotate function; |
|||
⚫ | |||
⚫ | |||
] |
|||
main :: IO () |
|||
main = bitwise 255 170</lang> |
main = bitwise 255 170</lang> |
||
{{Out}} |
|||
<pre>170 |
|||
255 |
|||
85 |
|||
-256 |
|||
0 |
|||
0 |
|||
0 |
|||
0 |
|||
1121501860331520 |
|||
1069547520 |
|||
1121501860331520 |
|||
1069547520</pre> |
|||
If you were shifting Words (unsigned integers) instead of Ints, then the shift would be automatically logical shifts: |
If you were shifting Words (unsigned integers) instead of Ints, then the shift would be automatically logical shifts: |
||
import Data.Word |
import Data.Word |
||
print $ shiftL (-1 :: Word) 1 |
print $ shiftL (-1 :: Word) 1 |
||
print $ shiftR (-1 :: Word) 1 |
print $ shiftR (-1 :: Word) 1 |
||
=={{header|HicEst}}== |
=={{header|HicEst}}== |
||
There is no rotate and no shift support built in to HicEst |
There is no rotate and no shift support built in to HicEst |