[Haskell-cafe] Re: Looking for largest power of 2 <= Integer

Simon Marlow simonmarhaskell at gmail.com
Thu Dec 6 04:53:52 EST 2007


Dan Piponi wrote:
> There's a bit of work required to make this code good enough for
> general consumption, and I don't know much about Haskell internals.
> 
> (1) What is the "official" way to find the size of a word? A quick
> look at 6.8.1's base/GHC/Num.lhs reveals that it uses a #defined
> symbol.

I'm not 100% sure what you mean by "word" in this context, but assuming you 
mean the same thing as we do in GHC when we say "word" (a pointer-sized 
integral type), then one way is

   Foreign.sizeOf (undefined :: Int)

or

   Foreign.sizeOf (undefined :: Ptr ())

(in GHC these are guaranteed to be the same).

There's also

   Data.Bits.bitSize (undefined :: Int)

which might be more appropriate since you're using Data.Bits anyway.

Cheers,
	Simon


More information about the Haskell-Cafe mailing list