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

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

$
0
0

Expanding on Josh's benchmark...one can improve the clz as follows

/***************** clz2 ********************/#define NUM_OF_HIGHESTBITclz2(a) ((a)                              \                  ? (((1U) << (sizeof(unsigned)*8-1)) >> __builtin_clz(a)) \                  : 0)

Regarding the asm: note that there are bsr and bsrl (this is the "long" version). the normal one might be a bit faster.


Viewing all articles
Browse latest Browse all 36

Trending Articles



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