function to count the number of "1" of one byte

原创 2016年08月30日 14:02:14

function to count the number of "1" of one byte ,for example 5(0101)has two "1"

经常遇到的面试题,毫无疑问需要借助位操作:


unsigned int bitCount (unsigned int value) {
    unsigned int count = 0;
    while (value > 0) {           // until all bits are zero
        //if ((value & 1) == 1)     // check lower bit
        //    count++;
		count += value & 0x1;
        value >>= 1;              // shift bits, removing lower bit
		//printf("value:%d, count:%d\n", value, count);
    }
    return count;
}


版权声明:原创文章,转载请注明出处。

相关文章推荐

[分析总结:leetcode-Number of Digit One]寻找整数1到n之间所有数字中1出现的次数

leetcode原题链接:https://leetcode.com/problems/number-of-digit-one/ 题目描述: 要求寻找整数1到n之间所有数字中,1出现的次数。如给定...

【LeetCode-Hard-2】【Number of Digit One】【1~N中‘1’出现次数】

第二题,中等难度,看到正确率略小于0.2就做了 233 Number of Digit One 19.5% Medium Given an integer n, count ...

Where do you want to jmp today ? In one of my previous posts (part 1 of writing stack based buffer

In the first 2 parts of the exploit writing tutorial series, I have discussed how a classic stack bu...

#248 Count of Smaller Number

题目描述: Give you an integer array (index from 0 to n-1, where n is the size of this array, valu...

leetcode -- Number of Digit One -- 数学题,找规律

https://leetcode.com/problems/number-of-digit-one/参考http://www.cnblogs.com/grandyang/p/4629032.htmlh...

LeetCode-233.Number of Digit One

https://leetcode.com/problems/number-of-digit-one/ Given an integer n, count the total number o...

LeetCode No.233 Number of Digit One

LeetCode No.233 Number of Digit One

leetcode解题笔记:Number of Digit One

233.Number of Digit One Given an integer n, count the total number of digit 1 appearing in all non...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)