For example:
unsigned char mask1 = 0x55; //01010101
unsigned short int mask2 = 0x8055;//1000000001010101
unsigned short int res = 0; //0000000000000000
res = mask1 | mask2;
so what is res now in bits?
does it convert mask1 from 2 bytes to 4? and the "empty spaces" will be zeroes?
I mean, in logical terms it will work like that?
res = mask1 | mask2 = 01010101 | 1000000001010101
0000000001010101
| 1000000001010101
----------------
1000000001010101
The both operands of the expression
are converted to the type
int(orunsigned intif the typeintis unable to represent all values of the operands) due to the integer promotions preserving values stored in the operands.From the C Standard (6.5.12 Bitwise inclusive OR operator)
and
and (6.3.1.8 Usual arithmetic conversions)
and (6.3.1.1 Boolean, characters, and integers)
So for example the value
0x55stored in an object of the typeunsigned charinternally will be represented in an object of the typeintlike0x00000055provided that thesizeof( int )is equal to4. And the value0x8055stored in an object of the typeunsigned shortwill be represented internally like0x00008055In this assignment
the result will be converted back to the type
unsigned short.