[Haskell-beginners] Bit arithmetic in Haskell

Artyom Shalkhakov artyom.shalkhakov at gmail.com
Tue Dec 9 06:30:28 EST 2008


Hello,

I'm trying to do some bit arithmetic. Here's the function:

> import Data.Bits
> import Data.Word
>
> g :: Word32 -> [Word32]
> g x = [(x `shiftR` 24) .&. 0xFF,
>        (x `shiftR` 16) .&. 0xFF,
>        (x `shiftR` 8) .&. 0xFF,
>        x .&. 0xFF]

This function should give bytes for the given number, like this:

g 255 -> [0,0,0,255]
g 256 -> [0,0,1,255]
g 65535 -> [0,0,255,255]

In reality, I get very strange answers.

Can anybody help me please?

Regards,
Artyom Shalkhakov.


More information about the Beginners mailing list