代码如下,以后再做解释吧
#include <iostream>
using namespace std;
struct _byte // 这是重点
{
unsigned a:1;
unsigned b:1;
unsigned c:1;
unsigned d:1;
unsigned e:1;
unsigned f:1;
unsigned g:1;
unsigned h:1;
};
long get_bit_count( unsigned char cByte )
{
struct _byte *BYTE = (struct _byte*)&cByte;
return (BYTE->a+BYTE->b+BYTE->c+BYTE->d+BYTE->e+BYTE->f+BYTE->g+BYTE->h); // 这是重点
}
void main()
{
unsigned char c='z';
long l=0;
l=get_bit_count(c);
}
关于 二进制中1的个数的其他几个算法 主要是开拓思维