Misc.hs
1 2 {-# LANGUAGE Strict #-} 3 module Misc where 4 5 -------------------------------------------------------------------------------- 6 7 import Data.Bits 8 9 -------------------------------------------------------------------------------- 10 11 class ShowHex a where 12 showHex :: a -> String 13 14 printHex :: ShowHex a => a -> IO () 15 printHex x = putStrLn (showHex x) 16 17 -------------------------------------------------------------------------------- 18 -- Integer logarithm 19 20 -- | Largest integer @k@ such that @2^k@ is smaller or equal to @n@ 21 integerLog2 :: Integer -> Int 22 integerLog2 n = go n where 23 go 0 = -1 24 go k = 1 + go (shiftR k 1) 25 26 -- | Smallest integer @k@ such that @2^k@ is larger or equal to @n@ 27 ceilingLog2 :: Integer -> Int 28 ceilingLog2 0 = 0 29 ceilingLog2 n = 1 + go (n-1) where 30 go 0 = -1 31 go k = 1 + go (shiftR k 1) 32 33 --------------------------------------------------------------------------------