Reputation: 33
It looks as if the unary bitwise not operator (~) on uint16_t and uint8_t returns int, and not the type of its operand (same for unary -). Can someone give a reason why this is so?
It came as a surprise that ~uint16_t(0) < uint16_t(0)
I use g++ version 9.2.1 20191008 (Ubuntu 9.2.1-9ubuntu2) x86_64-linux-gnu
Upvotes: 3
Views: 334
Reputation: 2161
From C++11 Standard 5.3.1.8:
The operand of the unary - operator shall have arithmetic or unscoped enumeration type and the result is the negation of its operand. Integral promotion is performed on integral or enumeration operands. The negative of an unsigned quantity is computed by subtracting its value from 2^n, where n is the number of bits in the promoted operand. The type of the result is the type of the promoted operand.
So it states that integer promotion takes place for unary operators.
Upvotes: 4