The Haskell base
documentation says that "A Word is an unsigned integral type, with the same size as Int."
How can I take an Int
and cast its bit representation to a Word
, so I get a Word
value with the same bit representation as the original Int
(even though the number values they represent will be different)?
I can't use fromIntegral
because that will change the bit representation.
I could loop through the bits with the Bits
class, but I suspect that will be very slow - and I don't need to do any kind of bit manipulation. I want some kind of function that will be compiled down to a no-op (or close to it), because no conversion is done.
I want to use IntSet
as a fast integer set implementation - however, what I really want to store in it are Word
s. I feel that I could create a WordSet
which is backed by an IntSet
, by converting between them quickly. The trouble is, I don't want to convert by value, because I don't want to truncate the top half of Word
values: I just want to keep the bit representation the same.
int2Word#
/word2Int#
in GHC.Prim
perform bit casting. You can implement wrapper functions which cast between boxed Int
/Word
using them easily.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With