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

前端 未结 12 2074
梦谈多话
梦谈多话 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 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.

提交回复
热议问题