在C ++中设置(short int)的高位字节和低位字节

时间:2012-12-16 10:14:54

标签: c++ integer typeconverter

我有设置(短整数)的上下字节的问题。 请查看我的代码,它只返回1号,但必须返回55。

int number = 55;
int mynumber = 0;

//convert short int to char.
char upper = number >> 8;
char lower = number && 8;

//convert char to short int .
mynumber = (mynumber & 0xff00) | lower;
mynumber = (mynumber & 0x00ff) | (upper << 8);

printf("%i",mynumber);

1 个答案:

答案 0 :(得分:4)

lower计算错误:

char lower = number & 0xff;