Assign unsigned int to char

What will happen when assigning an unsigned int to char? Just as the code below:
1
2
3
4
5
6
unsigned int num = 0x11223344;
char* tmp = new char;
*tmp = num >> 24;//0x11
*tmp = num >> 16;//0x22
*tmp = num >> 8;//0x33
*tmp = num;//0x44 

It seems that only the last 8 bits and the other bits are ignored. I wonder what exactly happens.
Thanks for your help.
A prvalue of an integer type or of an unscoped (since C++11) enumeration type can be converted to any other integer type. If the conversion is listed under integral promotions, it is a promotion and not a conversion.

If the destination type is unsigned, the resulting value is the smallest unsigned value equal to the source value modulo 2n where n is the number of bits used to represent the destination type. That is, depending on whether the destination type is wider or narrower, signed integers are sign-extended or truncated and unsigned integers are zero-extended or truncated respectively.

If the destination type is signed, the value does not change if the source integer can be represented in the destination type. Otherwise the result is
. implementation-defined (until C++20)
. the unique value of the destination type equal to the source value modulo 2n where n is the number of bits used to represent the destination type. (since C++20).
https://en.cppreference.com/w/cpp/language/implicit_conversion#Integral_conversions
Topic archived. No new replies allowed.