An implicit conversion of the elevation of data bits starts with the following three-way pen question:
Signed char a = 0xe0;unsigned int b = a;unsigned char c = A;
Signed char a = 0xe0;if (a = = 0xe0) {printf ("haha");} else{printf ("Nono");}
unsigned int a = 6;int B = -20; (A + B > 6)? printf (">6"): printf ("<=6");
First of all, the computer memory parts of the operation is very simple operation of primary school students, hardware design is the simpler the better, the specific number is what, depends on how you interpret the number of the way. A good example is the result of a bit manipulation data overflow in this blog post.
First look at the first question:
First, any of the constant integers are of type int, which is the compiler default, so 0xe0 is represented as 0x00 E0 (32-bit), assigned to signed Char A, because char is 8 bits, so only the low 8-bit E0 is assigned to the past, when the assignment is only a copy of the bit data, There is no sign of any such considerations. So a gets the data is E0 (1110 0000), which is a to interpret these binary as signed char, so the result is a negative value.
unsigned int b = A This is a negative value of a to an int, so to extend, the extension is the same as the highest bit , so B is extended to 0xFF FF FF E0, but B is interpreted as unsigned int, So you get a big number.
Note: 0xe0 This constant integer is written directly, the compiler considers him to be 0x00 xx E0, here is not an extension, does not involve the type of promotion, is the default bit int type.
unsigned char c = A assigns a value to a variable of type char, here is the direct 8-bit binary intact copy past, but C gets the data no longer interpreted as signed char, but is interpreted according to the unsigned char type, So C is a very large integer.
Then look at the second question:
0XE0 directly by default is 0x00 E0, which defaults to a positive int type. When assigned to a, intercept the last 8-bit 0xe0 assigned to a, but a gets the 8 bits of data and interprets it as signed Char. So it's a negative number, so a is not equal to 0XE0. In a real comparison, a will do a bit of ascension because it is compared to a value of type int, so a will be promoted to 0xFF FF FF E0, of course not equal to 0x00 xx E0.
Finally, we look at the third problem:
The signed number and the unsigned number are calculated together, and do not assume that the following is still a short-to-long alignment. Previously we knew that if a short and an int worked together, the compiler would first convert the short to an int type intermediate variable and then perform the operation. But it's not the case that there are signed and unsigned numbers in the same operation.
First, the conversion between the number of symbols and the number of unsigned numbers. Unsigned numbers can be converted to signed numbers, signed numbers can also be converted to unsigned shaping, and the conversion between the General compiler does not give a warning, because this conversion does not involve bit loss, memory content will not change. It's just a different explanation for the memory.
int a = -3;unsigned int b = A; B= 65533//and vice versa.
There is a standard conversion in the compiler, which appears during the shaping operation.
The rules of the standard conversion are: Short to long turns, signed to unsigned turns. If the converted data is longer than the converted data, the conversion may lose bit data. Typically, the compiler gives a warning.
unsigned data and signed integers are operations that are signed integers aligned to unsigned integers. Here, we can summarize a rule: shaping number operations, always follow a principle: Small to large expansion.
From small arrivals: short, unsigned short, int, unsigned int.
So the topic will first convert B into a very large, because the highest number of negative numbers is 1, converted to unsigned, the 1 is very valuable, so a+b is a very large numbers, so the final result: >6.
To summarize:
When the data is bit-expanded, the first thing to determine is whether the number to be expanded is signed or unsigned, and then determines whether the extended high is a sign bit or 0.
Char ch=-1;printf ("%02x,%02x", ch, (unsigned char) ch);
The result of this printing is:ffffffff,ff.
Because%x is the hexadecimal way to output integer data, that is, int, so two to the CH to expand the high, but the way is not the same, ch if there is a symbol in the high-level expansion of the number of the sign bit, if CH is unsigned, it is directly in the high position expansion 0.
Implicit conversion of data bit elevation [bit operation]