Finding trailing 0s in a binary number

前端 未结 7 855
渐次进展
渐次进展 2021-01-06 00:32

How to find number of trailing 0s in a binary number?Based on K&R bitcount example of finding 1s in a binary number i modified it a bit to find the trailing 0s.

相关标签:
7条回答
  • 2021-01-06 01:08
    int countTrailZero(unsigned x) {
        if (x == 0) return DEFAULT_VALUE_YOU_NEED;
        return log2 (x & -x);  
    }
    

    Explanation:

    x & -x returns the number of right most bit set with 1.

    e.g. 6 -> "0000,0110", (6 & -6) -> "0000,0010"

    You can deduct this by two complement: x = "a1b", where b represents all trailing zeros. then

    -x = !(x) + 1 = !(a1b) + 1 = (!a)0(!b) + 1 = (!a)0(1...1) + 1 = (!a)1(0...0) = (!a)1b 
    

    so

    x & (-x) = (a1b) & (!a)1b = (0...0)1(0...0)

    you can get the number of trailing zeros just by doing log2.

    0 讨论(0)
  • 2021-01-06 01:10

    Here's a way to compute the count in parallel for better efficiency:

    unsigned int v;      // 32-bit word input to count zero bits on right
    unsigned int c = 32; // c will be the number of zero bits on the right
    v &= -signed(v);
    if (v) c--;
    if (v & 0x0000FFFF) c -= 16;
    if (v & 0x00FF00FF) c -= 8;
    if (v & 0x0F0F0F0F) c -= 4;
    if (v & 0x33333333) c -= 2;
    if (v & 0x55555555) c -= 1;
    
    0 讨论(0)
  • 2021-01-06 01:13

    We can easily get it using bit operations, we don't need to go through all the bits. Pseudo code:

    int bitcount(unsigned x) {
        int xor = x ^ (x-1); // this will have (1 + #trailing 0s) trailing 1s
        return log(i & xor); // i & xor will have only one bit 1 and its log should give the exact number of zeroes
    }
    
    0 讨论(0)
  • 2021-01-06 01:18

    I think your method is working (allthough you might want to use unsigned int). You check the last digit each time, and if it's zero, you discard it an increment the number of trailing zero-bits.

    I think for trailing zeroes you don't need a loop.

    Consider the following:

    • What happens with the number (in binary representation, of course) if you subtract 1? Which digits change, which stay the same?
    • How could you combine the original number and the decremented version such that only bits representing trailing zeroes are left?

    If you apply the above steps correctly, you can just find the highest bit set in O(lg n) steps (look here if you're interested in how to do).

    0 讨论(0)
  • 2021-01-06 01:22

    Should be:

    int bitcount(unsigned char x)
    {
      int b;
      for(b=0; b<7; x>>=1)
      {
        if(x&1)
          break;
        else
          b++;
      }
      return b;
    }
    

    or even

    int bitcount(unsigned char x)
    {
      int b;
      for(b=0; b<7 && !(x&1); x>>=1) b++;
      return b;
    }
    

    or even (yay!)

    int bitcount(unsigned char x)
    {
      int b;
      for(b=0; b<7 && !(x&1); b++) x>>=1;
      return b;
    }
    

    or ...

    Ah, whatever, there are 100500 millions methods of doing this. Use whatever you need or like.

    0 讨论(0)
  • 2021-01-06 01:30

    On GCC on X86 platform you can use __builtin_ctz(no) On Microsoft compilers for X86 you can use _BitScanForward

    They both emit a bsf instruction

    0 讨论(0)
提交回复
热议问题