## Trailing Zero-Bits

### July 7, 2020

Today’s exercise indulges in some bit-hackery:

Given a positive integer, count the number of trailing zero-bits in its binary representation. For instance, 18

_{10}= 10010_{2}, so it has 1 trailing zero-bit, and 48_{10}= 110000_{2}, so it has 4 trailing zero-bits.

Your task is to write a program that counts the number of trailing zero-bits in the binary representation of a positive integer. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

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