How do I count the number of zero bits in an integer?

前端 未结 12 2078
梦谈多话
梦谈多话 2021-02-02 00:25

How would i go about finding the number of \'zero\' bits in C++. Suppose I have an integer;

int value = 276; 

For which I have the bits 100010

相关标签:
12条回答
  • 2021-02-02 00:47

    By far the most obvious solution is a lookup table.

    /* Assuming CHAR_BITS == 8 */
    int bitsPerByte[256] = { 8, 7, 7, 6, /* ... */ };
    int bitsInByte(unsigned char c) { return bits[c]; }
    
    0 讨论(0)
  • 2021-02-02 00:48

    According to me , the simplest way to get the zero bit count in a positive integer is the following piece of code.

    int get_zero_bit_count(int num)
    {
        int cnt = 0;
    
        while(num > 0)
            {
                int and_num = num & 1;
    
                if (and_num != num) cnt++;
    
                num >>= 1; 
            }
    
            return cnt;
        }
    

    This piece of code is easy to understand and is selp explainatory . This works well for positive integers.

    0 讨论(0)
  • 2021-02-02 00:56

    If you use GCC, you can try built-in functions:

    int __builtin_popcount (unsigned int x) 
    int __builtin_ctz (unsigned int x)
    int __builtin_clz (unsigned int x)
    

    See GCC Documentation for details.

    0 讨论(0)
  • 2021-02-02 00:56

    Do a one's compliment then count the 1s.

    count_zero_bits( x ) = count_one_bits( ~x );

    Implement the code to count the ones.

    template< typename I > 
    int count_one_bits( I i )
    {
       size_t numbits = 0;
       for( ; i != 0; i >>= 1 )
       {
          numbits += i&1;
       }
    }
    

    although there is an issue with my function if i is a negative number because >> will put 1 bits into the right hand side so you will get a never-terminating loop. If there is a templated way to enforce an unsigned type that would be ideal.

    Once you have that then:

    template< typename I > int count_zero_bits( I i )
    {
       return count_one_bits( ~i );
    }
    

    will work.

    0 讨论(0)
  • 2021-02-02 00:56
    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long int
    
    
    int main() {
        ll a;
        cin>>a;
        ll ones=__builtin_popcountll(~a);
        ll zero=__builtin_clzll(a);
        ll num=abs(ones-zero);
        cout<<num<<"\n";
        return 0;
    }
    

    with the help of builtin gcc functions i.e. popcount(counts number of set bit) and clz(count number of leading zero's) we can calculate the number of zero bit in an integer

    you can read about them here https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html, https://www.geeksforgeeks.org/builtin-functions-gcc-compiler/

    0 讨论(0)
  • 2021-02-02 01:00

    There is a great book for this kind of stuff : Hacker's Delight (yeah, the name sucks : it has nothing to do with security but exclusively bit-twiddling). It provides several algorithms to count '1' bits, the best can also be found here (although the book has explanations that this website doesn't).

    Once you know the '1' bits count, just subtract it to the number of bits in your type representation.

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