根据 cppreference ,
For signed
a
, the value ofa << b
isa * 2^b
if it is representable [in the unsigned version of the (since C++14)] return type [(which is then converted to signed: this makes it legal to createINT_MIN
as1 << 31
) (since C++14)], otherwise the behavior is undefined.
我不太明白这个规范。 如果它在返回类型的无符号版本中是可表示的究竟意味着什么?它仅适用于有符号值非负的情况吗?是INT_MIN << 1
和 -1 << 31
定义明确?
最佳答案
如果我们查看源代码,即 C++14 标准,我们会发现以下内容(突出显示有关无符号类型的部分):
The value of
E1 << E2
isE1
left-shiftedE2
bit positions; vacated bits are zero-filled. IfE1
has an unsigned type, the value of the result isE1 × 2
E2
, reduced modulo one more than the maximum value representable in the result type. Otherwise, ifE1
has a signed type and non-negative value, andE1 × 2
E2
is representable in the corresponding unsigned type of the result type, then that value, converted to the result type, is the resulting value; otherwise, the behavior is undefined.
对于 std::numeric_limits<int>::digits
所在的平台是31,执行1 << 31
是合法的. unsigned int
中的结果值将是 0x80000000
或 2147483648
.但是,该数字超出了 int
的有效值范围。对于这样的平台。但是,该位模式在被视为二进制补码表示时 int
等于-2147483648
,与 INT_MIN
相同对于这样一个平台。
如果你使用
int a = 2 << 31;
自 2 * 2
起您调用未定义的行为 31
不能表示为 unsigned int
在那个平台上。
Are INT_MIN << 1 and -1 << 31 well-defined?
不,他们不是。按位左移负数是未定义的行为。请注意在上面突出显示的文本中使用了非负值。
关于c++ - 了解 C++14 中有符号类型的按位左移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35110405/