一个简单的测试计算机的位数的程序: 不用sizeof

来源:互联网 发布:网络拓扑图怎么画好看 编辑:程序博客网 时间:2024/06/03 21:11
题目比较简单, 不再赘述, 只贴程序。
#include <iostream>using namespace std;int countBits() {    unsigned int i = 1;    int c = 1; // 注意是1, 不是0    while(i<<=1) {      c++;    }    return c;}/* Program to test function countSetBits */int main() {    cout << countBits() << endl;    return 0;}


关键是对c初始化为1, 运行结果如下:

0 0