如何使用移位运算符将字节组合成更大的整数
How bitwise shift operators are used to combine bytes into a larger integer
以下代码将两个字节合并为一个 16 位整数。
unsigned char byteOne = 0b00000010; // 2
unsigned char byteTwo = 0b00000011; // 3
uint16_t i = 0b0000000000000000;
i = (byteOne << 8) | byteTwo; //515
我正在尝试了解此代码为何有效。
如果我们将其分解并只关注一个字节,byteOne
;这是一个等于 00000010 的 8 位值。因此,将其左移 8 位应该始终得到 00000000
(因为从末尾移出的位会丢失),对吧?以下代码似乎就是这种情况:
uint8_t i = (byteOne << 8); // equal to 0, always, no matter what 8 bit value is assigned to byteOne
但如果这种思路是正确的,那么
uint16_t i = (byteOne << 8) | byteTwo;
应该等同于
uint16_t i = 0 | byteTwo; // Because 0b00000010 << 8 == 0b00000000
或者只是
uint16_t i = byteTwo; // Because 0b00000000 | 0b00000011 == 0b00000011
但它们并不等同,这让我很反感。在移位操作之前 byteOne
是否被 cast/converted 转换为 16 位 int?那可以解释这里发生了什么
0b0000000000000010 << 8 == 0b0000001000000000 // 512
如果 byteOne
在移位操作之前未被转换为 16 位整数,那么请解释为什么 (byteOne << 8)
在分配给 16 位整数时未计算为 0。
是——当您对小于 int
的任何值执行几乎任何类型的操作时,首先发生的事情是该值被提升为 int
(或者,在某些情况下, unsigned int
).
如果您真的关心此处适用的详细信息 (§[conv.prom]/1):
A prvalue of an integer type other than bool
, char16_t
, char32_t
, or wchar_t
whose integer conversion rank (6.8.4) is less than the rank of int
can be converted to a prvalue of type int
if int
can represent all the values of the source type; otherwise, the source prvalue can be converted to a prvalue of type unsigned int
.
然后操作发生在提升值 (§[expr.shift]/1):
The shift operators << and >> group left-to-right.
[...]
The operands shall be of integral or unscoped enumeration type and integral promotions are performed. The type of the result is that of the promoted left operand.
由于移位没有发生'inplace' (byteOne = byteOne << 8
),编译器需要为中间结果使用一个寄存器。
在 i = (byteOne << 8) | byteTwo;
行中,未指定中间寄存器的大小(例如使用强制转换)。只是最后的结果必须是uint16_t
。所以对于中间结果,它取决于编译器。
将截取的代码提供给编译器后,您可以获得以下汇编代码:
;// copy the two bytes and the word in the stack
movb , -1(%rbp) ;// uint8_t byteOne = 2
movb , -2(%rbp) ;// uint8_t byteTwo = 3
movw [=10=], -4(%rbp) ;// uint16_t i = 0
;// move the byteOne into the acumulate register(32bit)
movzbl -1(%rbp), %eax ;// uint32_t temp = byteOne
;// shift left by 8
sall , %eax ;// temp = temp << 8
;// move temp to different register
movl %eax, %edx ;// uint32_t temp2 = temp
;// move the byteTwo into the acumulate register(32bit)
movzbl -2(%rbp), %eax ;// temp = byteTwo
;// logical or of temp2 and temp
orl %edx, %eax ;// temp2 = temp2 | temp
;// copy back to stack location of i
movw %ax, -4(%rbp) ;// i = (uint16_t)temp2
%eax
是一个32位的寄存器,所以不会溢出。 uint16_t
的转换由 MOVWord movw %ax, -4(%rbp)
.
主动完成
我不确定编译器如何决定将哪个寄存器大小用于这些中间结果,但我怀疑这取决于您的系统和编译器。
我系统上的编译器g++.exe (x86_64-posix-seh-rev1, Built by MinGW-W64 project) 7.2.0
接缝使用 32 位寄存器作为标准。
以下代码也使用了 32 位寄存器,因此没有 return 预期的结果:
unsigned char byteOne = 0b00000010; // 2
unsigned char byteTwo = 0b00000011; // 3
uint16_t i = 0b0000000000000000;
i = ((byteOne << 32) | byteTwo << 24) >> 24; // 3
使用相同的32位%eax
寄存器,因此发生溢出。
因此,如果中间结果不超过 32 位,则结果与预期的一样:
unsigned char byteOne = 0b00000010; // 2
unsigned char byteTwo = 0b00000011; // 3
uint16_t i = 0b0000000000000000;
i = ((byteOne << 16) | byteTwo << 8) >> 8; // 515
8 位微控制器的编译器肯定会给出不同的结果。
以下代码将两个字节合并为一个 16 位整数。
unsigned char byteOne = 0b00000010; // 2
unsigned char byteTwo = 0b00000011; // 3
uint16_t i = 0b0000000000000000;
i = (byteOne << 8) | byteTwo; //515
我正在尝试了解此代码为何有效。
如果我们将其分解并只关注一个字节,byteOne
;这是一个等于 00000010 的 8 位值。因此,将其左移 8 位应该始终得到 00000000
(因为从末尾移出的位会丢失),对吧?以下代码似乎就是这种情况:
uint8_t i = (byteOne << 8); // equal to 0, always, no matter what 8 bit value is assigned to byteOne
但如果这种思路是正确的,那么
uint16_t i = (byteOne << 8) | byteTwo;
应该等同于
uint16_t i = 0 | byteTwo; // Because 0b00000010 << 8 == 0b00000000
或者只是
uint16_t i = byteTwo; // Because 0b00000000 | 0b00000011 == 0b00000011
但它们并不等同,这让我很反感。在移位操作之前 byteOne
是否被 cast/converted 转换为 16 位 int?那可以解释这里发生了什么
0b0000000000000010 << 8 == 0b0000001000000000 // 512
如果 byteOne
在移位操作之前未被转换为 16 位整数,那么请解释为什么 (byteOne << 8)
在分配给 16 位整数时未计算为 0。
是——当您对小于 int
的任何值执行几乎任何类型的操作时,首先发生的事情是该值被提升为 int
(或者,在某些情况下, unsigned int
).
如果您真的关心此处适用的详细信息 (§[conv.prom]/1):
A prvalue of an integer type other than
bool
,char16_t
,char32_t
, orwchar_t
whose integer conversion rank (6.8.4) is less than the rank ofint
can be converted to a prvalue of typeint
ifint
can represent all the values of the source type; otherwise, the source prvalue can be converted to a prvalue of typeunsigned int
.
然后操作发生在提升值 (§[expr.shift]/1):
The shift operators << and >> group left-to-right. [...] The operands shall be of integral or unscoped enumeration type and integral promotions are performed. The type of the result is that of the promoted left operand.
由于移位没有发生'inplace' (byteOne = byteOne << 8
),编译器需要为中间结果使用一个寄存器。
在 i = (byteOne << 8) | byteTwo;
行中,未指定中间寄存器的大小(例如使用强制转换)。只是最后的结果必须是uint16_t
。所以对于中间结果,它取决于编译器。
将截取的代码提供给编译器后,您可以获得以下汇编代码:
;// copy the two bytes and the word in the stack
movb , -1(%rbp) ;// uint8_t byteOne = 2
movb , -2(%rbp) ;// uint8_t byteTwo = 3
movw [=10=], -4(%rbp) ;// uint16_t i = 0
;// move the byteOne into the acumulate register(32bit)
movzbl -1(%rbp), %eax ;// uint32_t temp = byteOne
;// shift left by 8
sall , %eax ;// temp = temp << 8
;// move temp to different register
movl %eax, %edx ;// uint32_t temp2 = temp
;// move the byteTwo into the acumulate register(32bit)
movzbl -2(%rbp), %eax ;// temp = byteTwo
;// logical or of temp2 and temp
orl %edx, %eax ;// temp2 = temp2 | temp
;// copy back to stack location of i
movw %ax, -4(%rbp) ;// i = (uint16_t)temp2
%eax
是一个32位的寄存器,所以不会溢出。 uint16_t
的转换由 MOVWord movw %ax, -4(%rbp)
.
我不确定编译器如何决定将哪个寄存器大小用于这些中间结果,但我怀疑这取决于您的系统和编译器。
我系统上的编译器g++.exe (x86_64-posix-seh-rev1, Built by MinGW-W64 project) 7.2.0
接缝使用 32 位寄存器作为标准。
以下代码也使用了 32 位寄存器,因此没有 return 预期的结果:
unsigned char byteOne = 0b00000010; // 2
unsigned char byteTwo = 0b00000011; // 3
uint16_t i = 0b0000000000000000;
i = ((byteOne << 32) | byteTwo << 24) >> 24; // 3
使用相同的32位%eax
寄存器,因此发生溢出。
因此,如果中间结果不超过 32 位,则结果与预期的一样:
unsigned char byteOne = 0b00000010; // 2
unsigned char byteTwo = 0b00000011; // 3
uint16_t i = 0b0000000000000000;
i = ((byteOne << 16) | byteTwo << 8) >> 8; // 515
8 位微控制器的编译器肯定会给出不同的结果。