Trailing Zero-Bits
July 7, 2020
The easiest solution is to loop through the trailing bits:
(define (zerobits n) (let loop ((n n) (b 0)) (if (odd? n) b (loop (/ n 2) (+ b 1)))))
That takes time O(log n) to iterate through the bits of the number. A little trick using some bit-hackery and a lookup table gives us an O(1) solution:
(define (zerobits n) (vector-ref (vector 32 0 1 26 2 23 27 0 3 16 24 30 28 11 0 13 4 7 17 0 25 22 31 15 29 10 12 6 0 21 14 9 5 20 8 19 18) (modulo (bitwise-and n (- n)) 37)))
Who thinks up things like this? You can run the program at https://ideone.com/8hx3c7.
Cool little drill. Here is my take on it using Julia 1.4.1: https://pastebin.com/GbYGLYKV
First approach: create the bin string and work with that. Easier for figuring out what’s going on.
Second approach: just solve the task and forget about binary representation. Faster but opaque.
If you are just going to do fixed size integers, then the looping solution is constant time as well. For integers of any length, represented as bignums, I don’t think you can do better than some sort of loop over the the bits of the number.
The array solution is quite cute though (37 presumably is the lowest modulus m such that 2^i mod m is different for i in 0..31).
Python. Simple bit-testing.
def trail(n):
“”” “””
z = 0
while (n & 1) == 0:
z += 1
n >>= 1
return z
n = [18, 48]
for k in n:
print (“%d = %d” % (k, trail(k)))
[/code]
Sample output:
18 = 1
48 = 4[/code]
Again with proper indentation
I’ve been playing with Compiler Explorer lately. So I wrote three implementations in C and explored them there.
Link.
Then I wrapped a test harness around them to see how they performed. On my machine, tz2 and tz3 are about twice as fast as tz, wth a slight edge to tz3. You can see from Compiler Explorer that the compiler unrolled the loops in tz1 and tz3.
@kernelbob: how does this compare?
Or another nice one:
The rabbit hole always goes deeper…
After I posted, I found and read Our Gracious Host’s table lookup method, and then matthew asked about the __builtin_ctz function. __builtin_ctz is undefined for input zero, so I wrote two versions.
tz4 calls __builtin_ctz, and can’t be used for zero. (It generates a run-time error with clang’s “-fsanitize=undefined” flag.)
tz4z checks for zero explicitly, then calls __builtin_ctz on nonzero inputs.
tz5 uses table lookup mod 37.
Compiler Explorer.
Test and microbenchmark run. __builtin_ctz, with or without zero handling is fastest; table lookup is faster than any of my solutions.
A python solution:
Doesn’t work with zero.
Here’s another Haskell version having a few variants of the function. The first three work on arbitrary sized numbers (because popCount does).
Here’s a binary search solution in C.
Example:
$ echo 18 | awk -f /tmp/binary.awk
18 –> 1
$ echo 48 | awk -f /tmp/binary.awk
48 –> 4
Simple solution in C. In this the number of trailing zeros for 0 is the number of bits in an int.
function checkTrailingZeros($n){
// Take the number and convert to binary
$binary = decbin($n);
}
Oops, sorry for the bad paste. Will do it the right way next time round