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;
}
My output is the following.
24
-13
I tested larger numbers and operator <<
always gives me positive numbers, while operator ~
always gives me negative numbers. I then used sizeof()
and found...
When I use the left shift bitwise operator(
<<
), I receive an unsigned 4 byte integer.When I use the bitwise not operator(
~
), I receive a signed 4 byte integer.
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.
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!
Edit: 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.
uint8_t
as a number rather than a character? Are you sure your compiler does not alias that type toint
? – Feolauint8_t
to anint
. – Danelledanete