Zero Fill Right Shift in Haskell?

I have a little mess with Haskell (and you're enjoying me), and now I want to do a zero fill of the right shift .

I switched to hacks and found Data.Bits hacked by ghci , and here is what I typed (well, of course, I did not type -3):

:m Data.Bits shiftR (-9) 2 -3 
Fantastic, regular right shift, exactly what I expected.

OK, now I really read the documents. Ok, I see unsafeShiftR , but that’s not quite what I want.

So what have I missed? Is there a zero padding shift function for me in Haskell somewhere (i.e. the one that returns 1073741821 when -9 shifts right by 2)?

+6
source share
1 answer

Data.Bits takes into account signed types. However, since any null shift to the right with a nonzero shift will result in an unsigned type, you can simply convert your input into a word of the correct size:

 -- :m Data.Word -- shiftR ((fromIntegral x) :: Word32) 2 shiftR (-9 :: Word32) 2 1073741821 
+9
source

All Articles