什么是按位运算符和整数提升? [英] What is going on with bitwise operators and integer promotion?
问题描述
我有一个简单的程序。 请注意,我使用了大小为1字节的未签署固定宽度整数。
I have a simple program. Notice that I use an unsigned fixed-width integer 1 byte in size.
#include <cstdint>
#include <iostream>
#include <limits>
int main()
{
uint8_t x = 12;
std::cout << (x << 1) << '\n';
std::cout << ~x;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.get();
return 0;
}
我的输出如下。
24
-13
我测试了更大的数字,运算符<
总是给出正数,而运算符〜
我负数。然后我使用 sizeof()
并找到...
I tested larger numbers and operator <<
always gives me positive numbers, while operator ~
always gives me negative numbers. I then used sizeof()
and found...
当我使用左移位向运算符(
<
)时,我收到一个无符号的4字节整数。
When I use the left shift bitwise operator(
<<
), I receive an unsigned 4 byte integer.
当我使用bitwise not运算符(〜
)时,我收到一个有符号的4字节整数。
When I use the bitwise not operator(~
), I receive a signed 4 byte integer.
似乎bitwise不是运算符(〜
)算术运算符。但是,左移位运算符(< <
)似乎提升为无符号整数。
It seems that the bitwise not operator(~
) does a signed integral promotion like the arithmetic operators do. However, the left shift operator(<<
) seems to promote to an unsigned integral.
有义务知道编译器何时改变我背后的东西。如果我在我的分析是正确的,所有的位运算符提升到一个4字节整数?为什么有些签名和一些未签名?我很困惑!
I feel obligated to know when the compiler is changing something behind my back. If I'm correct in my analysis, do all the bitwise operators promote to a 4 byte integer? And why are some signed and some unsigned? I'm so confused!
编辑:我的假设总是得到积极或总是得到负值是错误的。但是,从错误的角度来看,我明白了下面这些伟大的答案真正发生了什么。
My assumption of always getting positive or always getting negative values was wrong. But from being wrong, I understand what was really happening thanks to the great answers below.
推荐答案
[expr.unary.op]
[expr.unary.op]
〜
的操作数应具有整数或无范围的枚举类型;
结果是其操作数的一个补码。 整合促销活动为
。
The operand of
~
shall have integral or unscoped enumeration type; the result is the one’s complement of its operand. Integral promotions are performed.
[expr.shift]
$ b
[expr.shift]
移位运算符
<
和 >>
组从左到右。 [...]操作数应为整数或无范围的枚举类型,并且执行整数促销。
The shift operators
<<
and>>
group left-to-right. [...] The operands shall be of integral or unscoped enumeration type and integral promotions are performed.
uint8_t
(幕后通常是 unsigned_char
)的整体推广是什么?
What's the integral promotion of uint8_t
(which is usually going to be unsigned_char
behind the scenes)?
[conv.prom]
[conv.prom]
除
bool
,char16_t
,char32_t
或
wchar_t
,其整数转换等级(4.13)小于
int
的等级可以转换为类型int
如果int
可以表示所有
源类型的值;否则,源prvalue可以
转换为unsigned int
的prvalue。
A prvalue of an integer type other than
bool
,char16_t
,char32_t
, orwchar_t
whose integer conversion rank (4.13) 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
.
所以 int
,因为 uint8_t
的所有值都可以用 int
。
So int
, because all of the values of a uint8_t
can be represented by int
.
什么是 int(12)< 1
? int(24)
。
〜int $ c>?
int(-13)
。
这篇关于什么是按位运算符和整数提升?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!