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 Protagonist for What is the fastest/most efficient way to find the highest set bit (msb) in an integer in C?

$
0
0

This should be lightning fast:

int msb(unsigned int v) {  static const int pos[32] = {0, 1, 28, 2, 29, 14, 24, 3,    30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19,    16, 7, 26, 12, 18, 6, 11, 5, 10, 9};  v |= v >> 1;  v |= v >> 2;  v |= v >> 4;  v |= v >> 8;  v |= v >> 16;  v = (v >> 1) + 1;  return pos[(v * 0x077CB531UL) >> 27];}

Viewing all articles
Browse latest Browse all 73

Trending Articles



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