您需要使用移位>>运算符:
unsigned long long x = static_cast<unsigned long long>(your_value);
//unsigned long long fix for issue pointed out by @Zac Howland in comments
unsigned int count = 0;//number of 1 bits
while (x != 0)
{
unsigned long long bit = x & 1;
if( bit == 1 )
{
count ++;//...
}
else //zero
{
//...
}
x >>= 1;
}
Run Code Online (Sandbox Code Playgroud)
还有其他方法以各种方式执行此操作,您可以在此处找到它们(以及其他内容)