C ++按位运算?

时间:2016-12-20 15:35:03

标签: c++ arrays bit-manipulation byte bit

假设我有一个u_char字节数组,如下所示:

+++++++++++++++++++++++++++++++++++++++++++++++++++++
u_char |hex_value|       |bit_size_in_array_position
+++++++++++++++++++++++++++++++++++++++++++++++++++++
bits[0] = 0xa0 -> 8 bits  8
bits[1] = 0xd3 -> 8 bits  16
bits[2] = 0xfd -> 8 bits  24
bits[3] = 0x6a -> 8 bits  32
bits[4] = 0xbc -> 8 bits  40
bits[5] = 0xe2 -> 8 bits  48
bits[6] = 0x04 -> 8 bits  56
bits[7] = 0x7f -> 8 bits  64
bits[8] = 0x06 -> 8 bits  72
bits[9] = 0x17 -> 8 bits  80
bits[10] = 0x5d -> 8 bits  88
bits[11] = 0xb7 -> 8 bits  96
bits[12] = 0x93 -> 8 bits  104
bits[13] = 0xea -> 8 bits  112
bits[14] = 0x08 -> 8 bits  120
bits[15] = 0x1c -> 8 bits  128
bits[16] = 0x4f -> 8 bits  136
bits[17] = 0x3a -> 8 bits  144
bits[18] = 0x00 -> 8 bits  152
bits[19] = 0xfa -> 8 bits  160

我需要从1 bytes数组的140th bit位置阅读u_char bits[20];。如果我没有错,它必须具有0xf3的十六进制值,但我无法得到正确的值。

我使用了以下按位操作:

((unsigned)(bits[16] & 0xF) << 8) + (unsigned)(bits[17] & 0xF0);

我刚刚开始使用C ++,这是我第一次使用按位操作。 这个操作有什么问题,如何纠正呢?

谢谢!

0 个答案:

没有答案