Commits

Iago Abal committed be7e1fe

Add `lsb1' function to find the least significant 1-bit

  • Participants
  • Parent commits 00a446e

Comments (0)

Files changed (1)

File Data/BitVector.hs

   , (@@), extract
   , (!.)
   , least, most
-  , msb, lsb, msb1
+  , msb, lsb, msb1, lsb1
   -- * Arithmetic
   , signumI
   , sdiv, srem, smod
   where go i | testBit a i = i
              | otherwise   = go (i-1)
 
+-- | Least significant 1-bit.
+--
+-- /Pre/: input must be non-zero.
+--
+-- >>> msb1 [4]3
+-- 0
+--
+-- >>> msb1 [4]6
+-- 1
+lsb1 :: BV -> Int
+lsb1 (BV _ 0) = error "Data.BitVector.lsb1: zero bit-vector"
+lsb1 (BV n a) = go 0
+  where go i | testBit a i = i
+             | otherwise   = go (i+1)
+
 ----------------------------------------------------------------------
 --- Arithmetic