Quantcast
Channel: What is the fastest/most efficient way to find the position of the highest set bit (msb) in an integer in C? - Stack Overflow
Viewing all articles
Browse latest Browse all 73

Answer by greggo for What is the fastest/most efficient way to find the position of the highest set bit (msb) in an integer in C?

$
0
0

Putting this in since it's 'yet another' approach, seems to be different from others already given.

returns -1 if x==0, otherwise floor( log2(x)) (max result 31)

Reduce from 32 to 4 bit problem, then use a table. Perhaps inelegant, but pragmatic.

This is what I use when I don't want to use __builtin_clz because of portability issues.

To make it more compact, one could instead use a loop to reduce, adding 4 to r each time, max 7 iterations. Or some hybrid, such as (for 64 bits): loop to reduce to 8, test to reduce to 4.

int log2floor( unsigned x ){   static const signed char wtab[16] = {-1,0,1,1, 2,2,2,2, 3,3,3,3,3,3,3,3};   int r = 0;   unsigned xk = x >> 16;   if( xk != 0 ){       r = 16;       x = xk;   }   // x is 0 .. 0xFFFF   xk = x >> 8;   if( xk != 0){       r += 8;       x = xk;   }   // x is 0 .. 0xFF   xk = x >> 4;   if( xk != 0){       r += 4;       x = xk;   }   // now x is 0..15; x=0 only if originally zero.   return r + wtab[x];}

Viewing all articles
Browse latest Browse all 73

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>