Calculation Problem between the number of signed characters and the number of unsigned characters
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:
Unsigned int A = 20;
Signed int B =-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:
Unsigned int A = 20;
Signed int B =-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:
Signed int B =-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.