I have:
(gdb) display/t raw_data[4]<<8
24: /t raw_data[4]<<8 = 1111100000000
(gdb) display/t raw_data[5]
25: /t raw_data[5] = 11100111
(gdb) display/t (raw_data[4]<<8)|raw_data[5]
26: /t (raw_data[4]<<8)|raw_data[5] = 11111111111111111111111111100111
Why is the result on line 26 not 0001111111100111
? Thanks.
edit: More specifically:
(gdb) display/t raw_data[5]
27: /t raw_data[5] = 11100111
(gdb) display/t 0|raw_data[5]
28: /t 0|raw_data[5] = 11111111111111111111111111100111
Why is the result on line 26 not 11100111
?
Your data type is a char
, which on your platform appears to be signed. The entry raw_data[5]
holds the negative number -25.
The print format t
prints the data as unsigned integer in binary. When you print raw_data[5]
, it is converted to the unsigned char 213, but has only 8 bits. When you do the integer arithmetic on the data, the chars are promoted to a 32-bit integer.
Promoting the negative char value -25 to a signed int will, of course, yield -25, but its representation as an unsigned int is now 2^^32 + x
, whereas as an unsigned char it was 2^^8 + x
. That's where all the ones at the beginning of the 32-bit binary number come from.
It's maybe better to work with unsigned raw data.
Let's just ignore the first block, since the second block is a minimal reproduction.
Also note that 0 | x
0 | x
preserves the value of x
, but causes the usual integral promotions.
Then the second block is not so unexpected.
(gdb) display/t raw_data[5]
27: /t raw_data[5] = 11100111
Ok, raw_data[5]
is int8_t(-25)
(gdb) display/t 0|raw_data[5]
28: /t 0|raw_data[5] = 11111111111111111111111111100111
and 0|raw_data[5]
is int(-25)
. Indeed, the value was preserved.
The constant 8 caused a promotion to a signed integer, so you're seeing sign extension as well at the promotion. Change it to UINT8_C(8). You'll need to include stdint.h for the macro.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.