How to get position of right most set bit in C

前端 未结 15 1904
你的背包
你的背包 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:06

    x & ~(x-1) isolates the lowest bit that is one.

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

    1- Subtract 1 form number: (a-1)

    2- Take it's negation : ~(a-1)

    3- Take 'AND' operation with original number:

    int last_set_bit = a & ~(a-1)

    The reason behind subtraction is, when you take negation it set its last bit 1, so when take 'AND' it gives last set bit.

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

    Check if a & 1 is 0. If so, shift right by one until it's not zero. The number of times you shift is how many bits from the right is the rightmost bit that is set.

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

    One can use the property of 2s-complement here.
    Fastest way to find 2s-complement of a number is to get the rightmost set bit and flip everything to the left of it.
    eg: consider a 4 bit system
    4=0100
    2s complement of 4 = 1100, which nothing but -4
    4&(-4)=0100.
    Notice that there is only one set bit and its at rightmost set bit of 4
    Similarly we can generalise this for n.
    n&(-n) will contain only one set bit which is actually at the rightmost set bit position of n.
    since there is only one set bit in n&(-n) , it is a power of 2.
    So finally we can get the bit position by:

    log2(n&(-n))+1

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

    This answer Unset the rightmost set bit tells both how to get and unset rightmost set bit for an unsigned integer or signed integer represented as two's complement.

    get rightmost set bit,

    x & -x
    // or
    x & (~x + 1)
    

    unset rightmost set bit,

    x &= x - 1
    // or
    x -= x & -x  // rhs is rightmost set bit
    

    why it works

    x:                     leading bits  1  all 0
    ~x:           reversed leading bits  0  all 1
    ~x + 1 or -x: reversed leading bits  1  all 0
    x & -x:                       all 0  1  all 0
    

    eg, let x = 112, and choose 8-bit for simplicity, though the idea is same for all size of integer.

    // example for get rightmost set bit
    x:             01110000
    ~x:            10001111
    -x or ~x + 1:  10010000
    x & -x:        00010000
    
    // example for unset rightmost set bit
    x:             01110000
    x-1:           01101111
    x & (x-1):     01100000
    
    0 讨论(0)
  • 2020-12-09 05:17

    I inherited this one, with a note that it came from HAKMEM (try it out here). It works on both signed and unsigned integers, logical or arithmetic right shift. It's also pretty efficient.

    #include <stdio.h>
    
    int rightmost1(int n) {
        int pos, temp;
        for (pos = 0, temp = ~n & (n - 1); temp > 0; temp >>= 1, ++pos);
        return pos;
    }
    
    int main()
    {
        int pos = rightmost1(16);
        printf("%d", pos);
    }
    
    0 讨论(0)
提交回复
热议问题