Result of bitwise operator in C++ -
Checking some compiler (comma, g ++) has confirmed that some "integer types" are the result of the bitwise operator int:
zero foo (unsigned char); Zero foo (signed short); Unsigned four A, B; Fu (A | B);
I could expect to be an unsigned four by the type of "a | b" because both operands are unsigned char, but the compiler says that the result is an int, and call To foo () is unclear why the language has been designed so that the result is an integer, or is this implementation dependent?
Thank you,
This is actually a standard C ++ behavior (ISO / IEC 14882):
5.13 / 1 bit wise inclusive or operator
Normal arithmetic conversion is performed; The result is that its operators have a bitwise inclusive or function. The operator only applies to integral or calculation operands.
5/9 Common Arithmetic Conversions
Many binary operators hope that generate arithmetic or computation type, The objective is to cultivate a common type, which is also the type of result. This method is called normal arithmetic conversion , which is defined as follows:
- If there is an operand type then
long double
, The other will be converted intolong double
.- Otherwise, if any operand is
double
, the second will be converted todouble
. .- Otherwise, if there is an operand
float
, the other will be converted tofloat
.- Otherwise, integral publicity
4.5 / 1 integral publicity
one Type the range
Can be converted into a value of. Due to the "natural" shape for the efficient environment for the arithmetic (see) execution environmentchar
,signed four
,unsigned char
,short int
, orsigned mini int < / Code> can be converted to a value of the code type if
int
can represent all values of the source type; Otherwise, the amount of source<< code> unsigned int