伊谢尔伦2017-04-17 13:10:09
will not put all 1's when converting. Please note that the following
is the binary representation of 2000. 0100 1110 0010 0000
After shortening the 1 byte
to char
, the following 0010 0000
should be retained. And this decimal representation is 32
PHP中文网2017-04-17 13:10:09
First of all, you need to read a book called D&E. Furthermore, this blame should fall on the C language.
C++ wanted to be compatible with the C language when it was designed, so the implicit relationship between char and int Formula conversion (without any compilation error warning) is brought by C. When an int larger than the maximum value of char is converted into char, the behavior should be 未定义行为
. Don’t go into the details, you just need to know how to write the code That’s not right.
PS:
The explanation given to you upstairs is just an implementation under a specific compiler. From the perspective of undefined behavior, it is legal no matter how it is implemented by any compiler.
I I remember that I used to use shift operations when writing programs. At that time, I was not very clear about the consequences of using signed integers. As a result, the program ran very well under gcc, but not under vc. It was not until I read the book later that all bit operations were Use uint32_t
or unint64_t
.
The value of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a non-negative value, the value of the result is the integral part of the quotient of E1/2E2. If E1 has a signed type and a negative value, the resulting value is implementation-defined.