计算一个数的二进制表示中有多少个1
2007-10-09 17:34
393 查看
这个阶段的我好像很喜欢把玩code。拿到Code会想它是否能够工作。先使用,再Trace,然后总结归纳。
这些都是从Points C中摘录的。不知道文章该该怎么分类。所以暂且归为翻译这一栏目。
现在看来还是觉得这些简短的Code很经典。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
*********************************************方法一**********************************************************
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
** This function returns the number of 1-bits that appeared in
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
** the argument value.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
count_one_bits( unsigned value )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int ones;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
** While the value still has some 1-bits in it ...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for( ones = 0; value != 0; value = value >> 1 )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
** If the low-order bit is a 1, count it.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if( value % 2 != 0 )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ones = ones + 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return ones;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
******************************************方法二*******************************************
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
** This function returns the number of 1-bits that appeared in
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
** the argument value.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
count_one_bits( unsigned value )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int ones;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
** While the value still has some 1-bits in it ...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for( ones = 0; value != 0; value >>= 1 )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
** If the low-order bit is a 1, count it.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if( ( value & 1 ) != 0 )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ones += 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return ones;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
这些都是从Points C中摘录的。不知道文章该该怎么分类。所以暂且归为翻译这一栏目。
现在看来还是觉得这些简短的Code很经典。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
*********************************************方法一**********************************************************
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
** This function returns the number of 1-bits that appeared in
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
** the argument value.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
count_one_bits( unsigned value )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int ones;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
** While the value still has some 1-bits in it ...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for( ones = 0; value != 0; value = value >> 1 )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
** If the low-order bit is a 1, count it.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if( value % 2 != 0 )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ones = ones + 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return ones;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
******************************************方法二*******************************************
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
** This function returns the number of 1-bits that appeared in
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
** the argument value.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
count_one_bits( unsigned value )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int ones;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
** While the value still has some 1-bits in it ...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for( ones = 0; value != 0; value >>= 1 )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
** If the low-order bit is a 1, count it.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if( ( value & 1 ) != 0 )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ones += 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return ones;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
相关文章推荐
- 计算一个十进制数的二进制表示有多少位1
- 191. Number of 1 Bits (计算一个整数的二进制表示中有多少个1)
- 计算一个整数的二进制表示有多少个1(别人的最快算法)
- 计算一个数的二进制表示中有多少个1
- 算法的强大——快速计算一个正二进制整数中包含多少个1
- c语言:输入两个整数m和n,计算需要改变m的二进制表示中的多少位才能得到n
- 算法的强大——快速计算一个正二进制整数中包含多少个1
- 计算一个字符的二进制表示中有几个1
- 输入一个整数,求该整数的二进制表达中有多少个1。例如输入10,由于其二进制表示为1010,有两个1,因此输出2。
- c语言:输入两个整数m和n,计算需要改变m的二进制表示中的多少位才能得到n
- 输入一个整数,求该整数的二进制表示中有多少个1
- Java算法总结:输入一个整数,求该整数的二进制表示中有多少个1
- 位操作:一个整数的二进制表示中 有多少个1?
- 给定一个整数N,那么N的阶乘N!末尾有多少个0呢?求N!的二进制表示中最低位1的位置。
- JS 二进制计算,如何得到一个负数的二进制表示
- 输入两个整数m和n,计算需要改变m的二进制表示中的多少位才能得到n。
- 巧妙计算一个数二进制表示法中1的位数
- 计算一个整数二进制表示时有多少位是为1的算法
- gcc __builtin_popcount 计算二进制表示中有多少1
- 计算将一个十进制整数转换成二进制含多少个1