Quantcast
Viewing latest article 23
Browse Latest Browse All 36

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

I had a need for a routine to do this and before searching the web (and finding this page) I came up with my own solution basedon a binary search. Although I'm sure someone has done this before! It runs in constant time and can be faster than the "obvious" solution posted, although I'm not making any great claims, just posting it for interest.

int highest_bit(unsigned int a) {  static const unsigned int maskv[] = { 0xffff, 0xff, 0xf, 0x3, 0x1 };  const unsigned int *mask = maskv;  int l, h;  if (a == 0) return -1;  l = 0;  h = 32;  do {    int m = l + (h - l) / 2;    if ((a >> m) != 0) l = m;    else if ((a & (*mask << l)) != 0) h = m;    mask++;  } while (l < h - 1);  return l;}

Viewing latest article 23
Browse Latest Browse All 36

Trending Articles



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