标准C没有这个类型,byte表示一个字节,对应C的unsigned char,你可以自己按下面的样子定义一个即可:
typedef unsigned char byte;
格式输出:
%o 八进制
%x 十六进制
接下来写一个利用byte计算二进制数中有多少个1的例子!!!(转换为16进制接收。)
#include<iostream>
using namespace std;
typedef unsigned char byte;
//*******************************************
int count1(byte v){
int num=0;
while(v){
if(v%2==1){
num++;
}
v=v/2;
}
return num;
}
//***************************************************
int count2(byte v){
int num=0;
while(v){
num+=v&0x01;
v>>=1;
}
return num;
}
//*************************************************
int count3(byte v){
int num=0;
while(v){
v=v&(v-0x01);
num++;
}
return num;
}
//*************************************************
int main(){
byte v;
while(scanf("%x",&v)!=EOF){
printf("%d\n",count2(v));
}
system("pause");
return 0;
}