c++ - 这个函数有什么作用?

标签 c++ c

我正在阅读一个包含以下功能的程序,即

int f(int n) {
    int c;
    for (c=0;n!=0;++c) 
        n=n&(n-1);
    return c;
}

我不太明白这个函数打算做什么?

最佳答案

counts number of 1's在 n 的二进制表示中

关于c++ - 这个函数有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3265942/

相关文章:

C++ 删除 base 或 dynamic_cast 指针?

C 窗口 sendto()

python - Python 的包装 DLL : "fatal error LNK1127: library is corrupt"

c - 为什么 %u %d 在 UNIX/LINUX 上不能用于打印地址

c - 如何在 C 程序中的 char 数组中分配输入(来自 echo 命令)

c - 警告 : assignment discards qualifiers from pointer target type

php - 高性能Web Spider的开发

C++ constexpr 实现差异

c++ - 如何处理 -Wreturn-type 以切换 C++11 枚举类?

c++ - 使用函数 boost Qi 编写规则