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出现的次数。如给定...

剑指offer 面试题32:从1到n整数中1出现的次数(leecode233. Number of Digit One) 题解

剑指offer 面试题32:从1到n整数中1出现的次数(Leecode233. Number of Digit One) 提交网址: http://www.nowcoder.com/prac...

【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 ...
  • hjy0125
  • hjy0125
  • 2015年07月21日 22:37
  • 335

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...

[LeetCode]233.Number of Digit One

题目Given an integer n, count the total number of digit 1 appearing in all non-negative integers less ...

Leetcode Number of Digit One

Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th...

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...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:function to count the number of "1" of one byte
举报原因:
原因补充:

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