可能简单的按位移位问题

时间:2019-06-15 13:18:41

标签: c bit-manipulation

unsigned short int temp, temp2;

temp = 0xbc61; // 1011110001100001
// temp2 has to be 0x61 (01100001) but I don't know how to shift, mask or whatever
temp2 = (temp << 8); // this doesnt work, because I get 0x6100 (110000100000000)

1 个答案:

答案 0 :(得分:3)

要选择1011110001100001的最低8位,您需要对11111111执行按位与运算:

  1011110001100001
& 0000000011111111
------------------
  0000000001100001

也就是说,

temp2 = temp & 0xff;  // or 255