191。Number of1 Bits
来源:互联网 发布:天龙八部辅助软件 编辑:程序博客网 时间:2024/05/28 23:09
/*
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).
For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3.
*/
#define max 1010
int hammingWeight(uint32_t n) {
uint32_t i,count = 0,temp = n;
int a[max] = {0};
for(i = 0 ; n!= 0 ; i++)
{
a[i] = n % 2;
n = n / 2;
}
for(i=0;temp != 0;i++)
{
temp = temp / 2;
if(a[i]==1)
count++;
}
return count;
}
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).
For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3.
*/
#define max 1010
int hammingWeight(uint32_t n) {
uint32_t i,count = 0,temp = n;
int a[max] = {0};
for(i = 0 ; n!= 0 ; i++)
{
a[i] = n % 2;
n = n / 2;
}
for(i=0;temp != 0;i++)
{
temp = temp / 2;
if(a[i]==1)
count++;
}
return count;
}
阅读全文
0 0
- 191。Number of1 Bits
- leetcode:bits:Number of 1 Bits(191)
- 191Number of 1 Bits
- 191 Number of 1 Bits
- 191 Number of 1 Bits
- 191Number of 1 Bits
- #191 Number of 1 Bits
- 191 Number of 1 Bits
- 191Number of 1 Bits
- [191]Number of 1 Bits
- 191 Number of 1 Bits
- 191Number of 1 Bits
- Number of 1 Bits---191
- [191] Number of 1 Bits
- leetcode-190&191 Reverse Bits & Number of 1 Bits
- [Leetcode 191, Easy] Number of 1 Bits
- leetCode #191 number of 1 bits
- LeetCode 191 Number of 1 Bits
- http协议详解
- 190。Reverse Bits
- ubuntu在vmware中静态(固定)IP地址的设置方法
- android 内核态
- java接口与抽象类
- 191。Number of1 Bits
- HDU 3032 Nim or not Nim?
- http和https区别
- Delphi中资源的简单应用
- getchar/getche/getch/gets/scanf/printf/puts/putchar
- SSD 源码分析
- 196. Delete Duplicate Emails
- HDU2255-奔小康赚大钱
- hibernate创建Configuration不同方法