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

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

提交回复
热议问题