Quantcast
Viewing latest article 28
Browse Latest Browse All 36

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

unsigned intmsb32(register unsigned int x){        x |= (x >> 1);        x |= (x >> 2);        x |= (x >> 4);        x |= (x >> 8);        x |= (x >> 16);        return(x & ~(x >> 1));}

1 register, 13 instructions. Believe it or not, this is usually faster than the BSR instruction mentioned above, which operates in linear time. This is logarithmic time.

From http://aggregate.org/MAGIC/#Most%20Significant%201%20Bit


Viewing latest article 28
Browse Latest Browse All 36

Trending Articles



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