function to count the number of "1" of one byte
2016-08-30 14:02
459 查看
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;
}
经常遇到的面试题,毫无疑问需要借助位操作:
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;
}
相关文章推荐
- Give a very good method to count the number of ones in a "n" (e.g. 32) bit number.
- count the number of "1" in the binary figure
- "Your computer could not be joined to the domain. You have exceeded the maximum number of computer accounts you are allowed to create in this domain. Contact your system administrator to have this limit reset or increased."
- Microsoft SQL Server 2008 R2 XML处理 One solution is to increase the number of characters retrieved from the server for XML data.
- How to count the occurrences of a number?
- python int to binary and count the number of 1
- Write a method to count the number of 2s between 0 and n
- how to print the spend time of one function
- Count the number of alphabets, spaces, digits and other characters in one line character
- How to count the number of threads in a process on Linux
- Selected solutions to exercise of "The C Programming Language" 2e (Part 3)
- The " count " function in map (STL)
- Restore SQL Server 2005 error : "The tail of the log for the database "TEST1" has not been backed up."
- Connector/Net 连接 MySQL远程服务器出现 "Unable to connect to any of the specified MySQL hosts" 解决方法
- Maximum number of things to show on the main page"
- Add a function called trim as a method of the prototype (similar to VBScript's Trim function)
- Selected solutions to exercise of "The C Programming Language" 2e (Part 7, End of Chapter 1)
- End of the "Road to DataStage Certification" D
- Selected solutions to exercise of "The C Programming Language" 2e (Part 4)
- Selected solutions to exercise of "The C Programming Language" 2e (Part 6)