Bitwise operations: Difference between revisions

Content deleted Content added
Added Algol W
Hout (talk | contribs)
→‎{{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 = do
bitwise a b =
mapM_
print $ a .&. b
print $ a .|. b
print
print $ a `xor` b
[ a .&. b
print $ complement a
, a .|. b
, a `xor` b
print $ shiftL a b -- left shift
, complement a
print $ shiftR a b -- arithmetic right shift
, shiftL a b -- left shift
print $ shift a b -- You can also use the "unified" shift function; positive is for left shift, negative is for right shift
, shiftR a b -- arithmetic right shift
print $ shift a (-b)
, shift a b -- You can also use the "unified" shift function;
print $ rotateL a b -- rotate left
--positive is for left shift, negative is for right shift
print $ rotateR a b -- rotate right
, shift a (-b)
print $ rotate a b -- You can also use the "unified" rotate function; positive is for left rotate, negative is for right rotate
, rotateL a b -- rotate left
print $ rotate a (-b)
, rotateR a b -- rotate right
, rotate a b -- You can also use the "unified" rotate function;
--positive is for left rotate, negative is for right rotate
, rotate a (-b)
]


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