判断1个字节中被置1的位的个数
#include <iostream> using namespace std; size_t calcHasOneBit(char ch) { size_t count = 0, i = 0; char val = 0; for (i = 0; i < 8; i++) { val = ch >> i; val &= 0x01; if (val) { count++; } } return count; } int main(int argc, char * argv[]) { cout << calcHasOneBit(‘a‘) << endl; return 0; }
时间: 2024-10-20 18:56:18