什么是按位运算符和整数提升? [英] What is going on with bitwise operators and integer promotion?

查看:185
本文介绍了什么是按位运算符和整数提升?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的程序。 请注意,我使用了大小为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, or wchar_t whose integer conversion rank (4.13) 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.

所以 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆