什么是0xFF?为什么将它移位24次? [英] What is 0xFF and why is it shifted 24 times?
问题描述
#define SwapByte4(ldata) \
(((ldata & 0x000000FF) << 24) | \
((ldata & 0x0000FF00) << 8) | \
((ldata & 0x00FF0000) >> 8) | \
((ldata & 0xFF000000) >> 24))
0x000000FF代表什么?我知道十进制15以F表示为十六进制,但是为什么<< 24?
What does that 0x000000FF represent? I know that decimal 15 is represented in hex as F, but why is it << 24?
推荐答案
这是一个十六进制值0x12345678,以二进制形式编写,并带有一些位位置的注释:
Here is a hex value, 0x12345678, written as binary, and annotated with some bit positions:
|31 24|23 16|15 8|7 bit 0|
+---------------+---------------+---------------+---------------+
|0 0 0 1 0 0 1 0|0 0 1 1 0 1 0 0|0 1 0 1 0 1 1 0|0 1 1 1 1 0 0 0|
+---------------+---------------+---------------+---------------+
...这是0x000000FF:
...and here is 0x000000FF:
+---------------+---------------+---------------+---------------+
|0 0 0 0 0 0 0 0|0 0 0 0 0 0 0 0|0 0 0 0 0 0 0 0|1 1 1 1 1 1 1 1|
+---------------+---------------+---------------+---------------+
因此按位与运算仅选择原始值的后8位:
So a bitwise AND selects just the bottom 8 bits of the original value:
+---------------+---------------+---------------+---------------+
|0 0 0 0 0 0 0 0|0 0 0 0 0 0 0 0|0 0 0 0 0 0 0 0|0 1 1 1 1 0 0 0|
+---------------+---------------+---------------+---------------+
...并将其左移24位,即可将其从最低8位移至最高位:
...and shifting it left by 24 bits moves it from the bottom 8 bits to the top:
+---------------+---------------+---------------+---------------+
|0 1 1 1 1 0 0 0|0 0 0 0 0 0 0 0|0 0 0 0 0 0 0 0|0 0 0 0 0 0 0 0|
+---------------+---------------+---------------+---------------+
...十六进制为0x78000000.
...which is 0x78000000 in hex.
其他部分对输入的其余8位部分起作用:
The other parts work on the remaining 8-bit portions of the input:
0x12345678
& 0x000000FF
----------
0x00000078 << 24 = 0x78000000 (as shown above)
0x12345678
& 0x0000FF00
----------
0x00005600 << 8 = 0x00560000
0x12345678
& 0x00FF0000
----------
0x00340000 >> 8 = 0x00003400
0x12345678
& 0x00000000
----------
0x12000000 >> 24 = 0x00000012
| ----------
0x78563412
因此,总体效果是将32位值ldata
视为四个8位字节的序列,并颠倒它们的顺序.
so the overall effect is to consider the 32-bit value ldata
as a sequence of four 8-bit bytes, and reverse their order.
这篇关于什么是0xFF?为什么将它移位24次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!