How to get position of right most set bit in C

前端 未结 15 1905
你的背包
你的背包 2020-12-09 04:48
int a = 12;

for eg: binary of 12 is 1100 so answer should be 3 as 3rd bit from right is set.

I want the position of the last most set bit o

相关标签:
15条回答
  • 2020-12-09 05:18

    The leftmost bit of n can be obtained using the formulae: n & ~(n-1)

    This works because when you calculate (n-1) .. you are actually making all the zeros till the rightmost bit to 1, and the rightmost bit to 0. Then you take a NOT of it .. which leaves you with the following: x= ~(bits from the original number) + (rightmost 1 bit) + trailing zeros

    Now, if you do (n & x), you get what you need, as the only bit that is 1 in both n and x is the rightmost bit.

    Phewwwww .. :sweat_smile:

    http://www.catonmat.net/blog/low-level-bit-hacks-you-absolutely-must-know/ helped me understand this.

    0 讨论(0)
  • 2020-12-09 05:21

    Accourding to dbush's solution, Try this:

    int rightMostSet(int a){
          if (!a) return -1;  //means there isn't any 1-bit
          int i=0;
          while(a&1==0){ 
            i++; 
            a>>1;
          }
          return i;
        }
    
    0 讨论(0)
  • 2020-12-09 05:22

    Try this

    int set_bit = n ^ (n&(n-1));
    
    0 讨论(0)
  • 2020-12-09 05:26
    int main(int argc, char **argv)
    {
        int setbit;
        unsigned long d;
        unsigned long n1;
        unsigned long n = 0xFFF7;
        double nlog2 = log(2);
    
        while(n)
        {
            n1 = (unsigned long)n & (unsigned long)(n -1);
            d = n - n1;
            n = n1;
    
            setbit = log(d) / nlog2;
            printf("Set bit: %d\n", setbit);
        }
    
        return 0;
    }
    

    And the result is as below.

    Set bit: 0
    Set bit: 1
    Set bit: 2
    Set bit: 4
    Set bit: 5
    Set bit: 6
    Set bit: 7
    Set bit: 8
    Set bit: 9
    Set bit: 10
    Set bit: 11
    Set bit: 12
    Set bit: 13
    Set bit: 14
    Set bit: 15
    
    0 讨论(0)
  • 2020-12-09 05:27

    return log2(((num-1)^num)+1);

    explanation with example: 12 - 1100

    num-1 = 11 = 1011

    num^ (num-1) = 12^11 = 7 (111)

    num^ (num-1))+1 = 8 (1000)

    log2(1000) = 3 (answer).

    0 讨论(0)
  • 2020-12-09 05:29

    There is a neat trick in Knuth 7.1.3 where you multiply by a "magic" number (found by a brute-force search) that maps the first few bits of the number to a unique value for each position of the rightmost bit, and then you can use a small lookup table. Here is an implementation of that trick for 32-bit values, adapted from the nlopt library (MIT/expat licensed).

    /* Return position (0, 1, ...) of rightmost (least-significant) one bit in n.
     *
     * This code uses a 32-bit version of algorithm to find the rightmost
     * one bit in Knuth, _The Art of Computer Programming_, volume 4A
     * (draft fascicle), section 7.1.3, "Bitwise tricks and
     * techniques." 
     *
     * Assumes n has a 1 bit, i.e. n != 0
     *
     */
    static unsigned rightone32(uint32_t n)
    {
        const uint32_t a = 0x05f66a47;      /* magic number, found by brute force */
        static const unsigned decode[32] = { 0, 1, 2, 26, 23, 3, 15, 27, 24, 21, 19, 4, 12, 16, 28, 6, 31, 25, 22, 14, 20, 18, 11, 5, 30, 13, 17, 10, 29, 9, 8, 7 };
        n = a * (n & (-n));
        return decode[n >> 27];
    }
    
    0 讨论(0)
提交回复
热议问题