First of all, I encountered this problem when writing a simulator program. We have the following type conversion:
Int16_t v16s;
Uint16_t v16u;
Int32_t v32s;
Uint32_t v32u;
V16s = 0xf08b; v16u = (uint16_t) v16s; // What is the difference between v16s and v16u in binary representation? No.1
V32s = (int32_t) v16s; v32u = (uint32_t) v16s; // do v32s and v32u have the same binary representation at this time? No. 2
V16s = 0x0f8b; v32s = (int32_t) v16s; v32u = (uint32_t) v16s; // What are the values of v32s and v32u? No. 3
V32s = 0xffff0fb8; v16s = (int16_t) v32s; v16u = (uint16_t) v32s; // What are the values of v16s and v16u? No. 4
V16s = 0xf08b; v16u = 0xf08b; v32s = (int32_t) v16s; v32u = (uint32_t) v16u; // What are the values of v32s and v32u? No. 5
First, let us know the answer:
- No.1: What are the differences between v16s and v16u in binary representation?
There is no difference between the two in binary representation, both of which are 0xf08b. Therefore, if you do not perform an operation, it is unnecessary to perform forced type conversion between simple data types of the same length, the compiler will skip your work. If you don't believe it, you can view the generated Assembly Code. For the No. 1 problem, no code is generated for the conversion statement !!
- NO2: Does v32s and v32u have the same binary representation at this time?
The two are in the same binary representation! All are 0xfffff08b! Here, we may make a mistake in the v32u = (uint32_t) v16s statement. That's what I made. According to my error, 0xf08b → 0x0000f08b! This is not correct! I will tell you the reason later (of course, if your C language is good enough, you will think that I have made a minor child ^ _ ^ and take care of the later ones ).
- No. 3: What are the values of v32s and v32u?
In this case, the values of v32s and v32u are 0x00000f8b!
- No. 4: What are the v16s and v16u values at this time?
At this time, the v16s and v16u values are both 0x0fb8!
- No. 5: The value of v32s is 0xfffff08b, and the value of v32u is 0x0000f08b. During automatic type conversion, if the original number is the unsigned number, the value is 0 in the upper position during expansion. If it is a signed number, then the time sign bit for filling in a high position! This is a bit similar to the ">" operator. When the unsigned number is shifted to the right, it is filled with 0 in the upper position. When the number of signed characters is shifted to the right, it is filled with the symbol bit in the upper position.
Now, let me tell you why it is the above result. This requires us to review the C language operations on type conversion. There are two types of type conversion in C language: automatic type conversion (implicit type conversion, which can be completed by a compiler) and forced type conversion (you know what you want, so conversion ).
For automatic type conversion, the most common is mixed operation and value assignment, and another is type conversion of function values.
- Value assignment: automatically converts the expression type on the right of "=" to the variable type on the right of "=", for example, int A = 4.5; the value of A is actually 4!
- Hybrid Operation: An operation expression contains multiple types. In this case, type conversion is required. When the types of the operands on both sides of the operator are different, one of the operands must be converted to be the same as the type of the other operand before the operation can be performed.
The conversion operations adopt the principle of high or low, that is, the lower-level operations are first converted to the same type as the higher-level operations, and then computed. The result data type is the same as that of the higher-level operations.
High Double comment float
Zookeeper
Too long
Zookeeper
↑ Unsigned
Zookeeper
Low int character char, short
Automatic conversion sequence table
- Type conversion of function return values: int F1 () {return 36.8 ;}
Forced type conversion Operator
You can use the force type conversion operator to convert an expression to a required type:
For example:
(Double) A (convert a to double type)
(INT) (x + y) (convert the value of X + Y to an integer)
(Float) (5% 3) (convert the value of 5% 3 to the float type)
(INT) (1.5 + 2.3) =?
(INT) 1.5 + 2.3 =?
Note that the expression should be enclosed in parentheses. If it is written as (INT) x + y, it will only convert X to an integer and then add it to y.
Here, you may understand, oh, we used the syntax of forced type conversion, but the program is actually automatic type conversion! Therefore, the following statements are executed: v16s = 0xf08b; v32s = (int32_t) v16s; v32u = (uint32_t) v16s; then, the binary representation of v32s and v32u is the same!
The forced conversion from high to low is essentially a truncation operation, which only retains the required parts at the low end and directly discards the remaining parts. Therefore, for statements: v32s = 0xffff0fb8; v16s = (int16_t) v32s; v16u = (uint16_t) v32s; after execution, the binary representation of v16s and v16u is the same!
Note: I have always stressed whether their binary representation is the same, but not whether their values are the same, because they are the same binary representation, you think of it as the value of the signed number and the unsigned number. In most cases, they are different (except for the same highest bit as 0)
Hybrid Operation
A hybrid operation means that the objects involved in an operation in an expression are not of the same data type. For example:
2*3.1416 * r 3.1416 * r x 3.6 * A % 5/(* B) + 'F ';
If R is an int-type variable, A is a float-type variable, and B is a double-type variable, the data types involved in the preceding three expressions are integer, real, and complex, this expression is called a hybrid expression. A hybrid operation is required to solve the mixed expression. At this time, the primary problem is to convert the data involved in the operation.
The following is an example of type conversion for better understanding. The following variables are provided:
Int A, J, Y; float B; long d; double C;
Then the assignment statement:
Y = J + 'A' + a * B-c/d;
The operation order and implicit type conversion are as follows:
① Calculate a * B. Because variable B is of the float type, the system automatically converts variable A to the double type and variable A to the int Type During computation. The two computation objects must be of the same type, variable A must also be converted to double, and the calculation result is double.
② Because C is double type, convert d to double type, then calculate C/d, and the result is double type.
③ Calculate J + 'A'. Convert 'A' (char type) to an integer and then add it to J. The result is an integer.
④ Add the results of step 1 and step 2, convert the result (INT) of step 2 to the double type, and then perform the operation. The result is the double type.
⑤ Subtract the result of step 1 from the result of step 2 and the result is of the double type.
⑥ Assign a value to Y. Convert the double type of the result in step 1 to an integer (because y is an integer variable on the left side of the value assignment operation), and cut off the decimal part of the double type data, compress the data to the int type and assign values.
The type conversion in the preceding steps is automatically completed by the C language compiling system.