[Computer composition principle] A question about sign extension

 Reprinted: https://blog.csdn.net/jianwei2016/article/details/88423154

What is the decimal (true value) and hexadecimal value (complement machine number) of si, usi, i, ui output on a 32-bit machine?
short si = -32768;
unsigned short usi = si;
int i = si;
unsingned ui = usi;

The following is the program verification code and results:

#include <bits/stdc++.h>
using namespace std;

int main(){
    short si = -32768;
    unsigned short usi=si;
    int i = si;
    unsigned int ui=usi;
    printf("short:          decimal:%d  Hexadecimal:%x\n",si,si);
    printf("unsigned short: decimal:%d   Hexadecimal:%x\n",usi,usi);
    printf("int:            decimal:%d  Hexadecimal:%x\n",i,i);
    printf("unsigned int:   decimal:%d   Hexadecimal:%x\n",ui,ui);
    return 0;
}

Validation results:
Validation results

Part 1: Basic knowledge about the sign extension of integer data:

1. The short data type is extended to the long data type

1. The short data type to be extended is a signed number

Sign extension is performed, that is, the sign bit of the short data type is filled into the high byte of the long data type (that is, the part that is more than the short data type) to ensure that the expanded value remains unchanged.

1: char x=10001001b; short y=x; then the value of y should be 11111111 10001001b;
2: char x=00001001b; short y=x; then the value of y should be 00000000 00001001b;

2. The short data type to be extended is an unsigned number

Perform zero extension, that is, fill the high byte of the long data type with zeros.

1: unsigned char x=10001001b; short y=x; then the value of y should be 00000000 10001001b;
2: unsigned char x=00001001b; short y=x; then the value of y should be 00000000 00001001b;

Second, the long data type is reduced to the short data type

If the high byte of the long data type is all 1 or all 0, the low byte will be directly truncated and assigned to the short data type; if the high byte of the long data type is not all 1 or not all 0, the transfer will be An error occurred.

3. Mutual conversion between signed numbers and unsigned numbers in data types of the same length

Assign the data in the memory directly to the type to be converted, and the value size will change. When the short type is extended to the long type, but the short type and the long type are signed and unsigned, the type is expanded according to rule 1, and then the value in the memory is directly intact according to this rule Give to each other.

The second part: the solution to this problem

1. It is known that short type variable si = -32768, its binary code is 1000, 0000, 0000, 0000, and hexadecimal is ffff8000.
When it is converted to unsigned short type variable usi , there is a sign in the data type of the same length. The mutual conversion of numbers and unsigned numbers means that the digits are compressed.

From the above basic knowledge, we can know that in the process of conversion, the highest bit of si loses the meaning of the sign bit and becomes a data bit. Therefore, the hexadecimal number of usi after conversion is 8000, which is 32768.

In order to better explain the above answer, we changed the number of the above question to -16 to verify our conclusion

Verification result 2
In the figure, we can see that the hexadecimal of -16 is fffffff0. After being converted to unsigned short, the high bit is lost and the sign bit meaning is lost. Finally, fff0 is the value of the variable usi, which is 65520.

2. When converted to int type, the short data type is converted to long data type, and the extended short data type is a signed number, so sign extension is performed, that is, the sign bit of the short data type is filled into the high word of the long data type Section bits (that is, the part that is more than the short data type), to ensure that the expanded value remains unchanged.

The original number short si = 10000000, 00000000 is
converted to int i = 11111111, 11111111, 10000000, 00000000

Sign extension, the value remains unchanged

3. When converted to unsigned int type, the short data type is converted to long data type, and the extended short data type is an unsigned number, so zero extension is performed, that is, the high byte of the long data type is filled with zeros .

The original number unsigned short usi = 10000000,
00000000 is converted to unsigned int ui = 00000000, 00000000, 10000000, 00000000

So the value does not change ui = 32768

references

On the issue of the sign bit extension of integer data
. Principles of Computer Composition (2nd Edition), Tang Shuofei, Higher Education Press

Guess you like

Origin blog.csdn.net/modi000/article/details/113747859