Bit hacks to compute floor(log2(int))

I wrote a program in the last day to determine the fastest method of seven implementations that I found over the Web that solves the operation “floor(log2(int))”. This operation takes an integer and determines the position of the topmost bit that is one. So, for example floor(log2(0x2)) would be 1, floor(log2(0x52)) would be 6. This operation is important in finding the nearest common ancestor of a tree (more on this in a book that I am writing).
Continue reading “Bit hacks to compute floor(log2(int))”