First bit index

I can see in http://hackage.haskell.org/package/base-4.7.0.0/docs/Data-Bits.html#v:bit how to convert from Int, n, to a bit that has the nth bit fixed using

bit :: Int -> a

However, how can I do the opposite of this? (Assuming the input bit has only 1 bit in it?)

+4
source share
2 answers

Since base-4.8.0.0 is

countLeadingZeros :: FiniteBits b => b -> Int
countTrailingZeros :: FiniteBits b => b -> Int

These indices are the most significant and least significant bits of the set, starting with the most significant and least significant ends, respectively. Subtract from finiteBitSize :: FiniteBits b => b -> Intto count from the other end.

+7
source

popCount $ x-1 , . 1 , .


, : popCount $ complement x .&. (x-1)

- , ANDing x , ( , ).

+3

Source: https://habr.com/ru/post/1683288/


All Articles