What is going on with bitwise operators and integer promotion?

后端 未结 4 1546
别跟我提以往
别跟我提以往 2020-11-28 15:32

I have a simple program. Notice that I use an unsigned fixed-width integer 1 byte in size.

#include 
#include 

        
4条回答
  •  眼角桃花
    2020-11-28 16:24

    [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]

    The shift operators << and >> group left-to-right. [...] The operands shall be of integral or unscoped enumeration type and integral promotions are performed.

    What's the integral promotion of uint8_t (which is usually going to be unsigned_char behind the scenes)?

    [conv.prom]

    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.

    So int, because all of the values of a uint8_t can be represented by int.

    What is int(12) << 1 ? int(24).

    What is ~int(12) ? int(-13).

提交回复
热议问题