博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Algorithm3: 获得一个int数中二进制位为1 的个数
阅读量:6855 次
发布时间:2019-06-26

本文共 319 字,大约阅读时间需要 1 分钟。

获得一个int数中二进制位为1 的个数
 
int 
NumberOfOne(
int 
n
){
                 
int 
count = 0;
                 
unsigned 
int 
flag = 1;
                 
while 
(flag){
                                 
if 
(
n 
& flag){
                                                count++;
                                }
                                flag = flag << 1;
                }
                 
return 
count;
}
int 
NumberOfOne2(
int 
n
){
                 
int 
count = 0;
                 
while 
(
n 
)
                {
                                count++;
                                 
n 
= 
n 
&(
n 
- 1);
                }
                 
return 
count;
}

转载于:https://www.cnblogs.com/hbhzsysutengfei/p/3414583.html

你可能感兴趣的文章