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

后端 未结 27 2615
终归单人心
终归单人心 2020-11-22 03:35

If I have some integer n, and I want to know the position of the most significant bit (that is, if the least significant bit is on the right, I want to know the position of

27条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 04:08

    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;
    }
    

提交回复
热议问题