Answers to The index of the first bit ( 2 )

  1. 2017-08-08 23:08

    popCount $ x-1 does that, effectively by counting the number of trailing zeroes. Subtracting 1 turns the trailing zeroes into ones and resets the only one that should be there.


    This is easy to adapt to a more general case without the assumption that only one bit of the input is set: popCount $ complement x .&. (x-1)

    The main idea is the same, and the ANDing with the complement of x gets rid of the ones that aren't newly created by the subtraction (which are the only ones that should be counted).

  2. 2017-08-09 00:08

    Since base-4.8.0.0 there are

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

    Those index the most-significant and least-significant set bits starting from the most-significant and least-significant ends respectively. Subtract from finiteBitSize :: FiniteBits b => b -> Int to count from the other end.

Leave a reply to - The index of the first bit

◀ Go back