cout <( 1 - 2 )
//
1 normal, all are signed numbers cout <(unsigned
int )
1 -
2 )
//
0-2 is converted to an unsigned number. cout <(unsigned
int )
1 -
2 .)
//
1 float does not have an unsigned number. Therefore, the unsigned number must be greater than the negative number of the float type!
The following experiments run in virual C ++ 6
Test whether the C LanguageAutomatic Integer Conversion PrincipleSome developers know little about these things. All operands are automatically convertedUnsigned type. Therefore, in this sense, the computing priority of the unsigned number is higher than that of the signed number, which is very important for embedded systems that should frequently use the unsigned data type.
First, conduct an experiment to define a signed int type data and an unsigned int type data respectively, and then compare the size:
UnsignedIntA =20; SignedIntB =-130;
A> B? Or B>? The experiment proves that B> A, that is,-130> 20, why is this result?
This is because in C language operations, if there is an operation between the unsigned number and the signed number, the compiler will automatically convert it to the unsigned number for processing. Therefore, a = 20, B = 4294967166. In this case, B>.
Another example:
UnsignedIntA =20; SignedIntB =-130; STD: cout<A + B <STD: Endl;
The result output is 4294967186. Similarly, before calculation, a = 20 and B are converted to 4294967166, so a + B = 4294967186
Subtraction is similar to multiplication.
If B =-130 is used as the signed int data, the operation between B and the immediate number does not affect the type of B, and the operation result is still signed INT:
SignedIntB =-130; STD: cout<B +30<STD: Endl;
The output is-100.
For floating point numbers, float (double) is actually a signed number. The unsigned and signed prefixes cannot be added to float and double, of course, there is no conversion problem between the unsigned numbers.
Original article address:Http://blog.csdn.net/zdl1016/article/details/2507743
Additional reading: http://www.52rd.com/Blog/Detail_RD.Blog_lentil_13982.html