移位运算符导致的错误
int val=(('D' << 24) + ('B' << 16) + ('E' << 8) + 'C')
c/c++ 规定,'D' 是一个字节大小, 移位运算的时候,如果右边的数字 》= 左边的数字所占的位数,那么就是错误的
如此说来,这条语句有问题吗?
应该把'D' 看成字符还是看成一个int?
[解决办法]
C++中移位操作也会进行整数提升的
C++98
5.8 Shift operators
The shift operators << and >> group lefttoright.
shiftexpression:
additiveexpression
shiftexpression << additiveexpression
shiftexpression >> additiveexpression
The operands shall be of integral or enumeration type and integral promotions are performed. The type of
the result is that of the promoted left operand. The behavior is undefined if the right operand is negative, or
greater than or equal to the length in bits of the promoted left operand.