Storage of integers in memory, promotion of integers

Preface: We know that the creation of a variable needs to open up space in the memory, and the size of the space is determined according to different types, so how is the data stored in the allocated memory? Here we discuss how integers are stored in memory.

Original code, inverse code, complement code

First of all, we need to know that there are 3 binary representation methods for integers in computers, namely 原码 反码 补码. These three representation methods are divided intosign bitandNumerical digitsIn the two parts, the sign bit uses 0 to represent "positive" and 1 to represent "negative", while the representation of the value bit is slightly different for positive and negative numbers.

An integer occupies 4 bytes in memory, and a byte (byte) is 8 bits (bit), so an integer occupies 32 bits, so it can be represented by a 32-bit binary number. And the first bit issign bit, and the remaining 31 bits areNumerical digits

The original inverse complement representation of positive and negative numbers

The original inverse complement form of positive numbers is the same.
The original inverse complement form of negative numbers varies

Original code: The original code can be obtained by directly translating the value into binary in the form of positive and negative numbers.
Inverse code: The sign bit of the original code remains unchanged, and the other bits are inverted bit by bit to get the complement code.
Complement code: Add 1 to the complement code to get the complement code

Examples are as follows:

	int a = 10;
//	00000000 00000000 00000000 00001010 - 原码
//	00000000 00000000 00000000 00001010 - 反码
//	00000000 00000000 00000000 00001010 - 补码

	int b = -10;
//	10000000 00000000 00000000 00001010 - 原码
//	11111111 11111111 11111111 11110101 - 反码 - 符号位不变,其他位按位取反
//  11111111 11111111 11111111 11110110 - 补码 - 在反码的基础上加1

It should be noted that: integers are stored in memory asComplementstored in the form, while the output isoriginal codeform output.

integer promotion

Let's look at this code below:

	char a = -1;
	//  变量a的类型是char,在内存中占一个字节,而-1是个整型值,在内存中占4个字节,那么-1该如何存储呢?
	signed char b = -1;
	//  有符号呢?
	unsigned char c = -1;
	//  无符号呢?
	printf("a=%d,b=%d,c=%d", a, b, c);

What do you think the answers are?
The answer after running is as follows:
insert image description here
Why is the output like this? Parsed as follows:

	char a = -1;
//  首先将-1的补码写出来
//  10000000 00000000 00000000 00000001   -1的原码
//  11111111 11111111 11111111 11111110   -1的反码
//  11111111 11111111 11111111 11111111   -1的补码
//  char在内存中占1个字节,即8个比特位,所以发生截断,取-1补码的后8位数 11111111
//  11111111放入c中,但我们打印的是有符号整型(%d),所以需要32个比特位,即需补上24个比特位
//  整型提升,符号位上是几,咱们就补几,11111111的符号位上是1,所以我们补1
//  11111111 11111111 11111111 11111111  补后的补码
//  11111111 11111111 11111111 11111110   反码
//  10000000 00000000 00000000 00000001   原码
//  读原码为-1,则输入-1

   signed char b = -1;
 //同上  

   unsigned char c = -1;
//  首先将-1的补码写出来
//  10000000 00000000 00000000 00000001  -1的原码
//  11111111 11111111 11111111 11111110  -1的反码
//  11111111 11111111 11111111 11111111  -1的补码
//  char在内存中占1个字节,即8个比特位,所以发生截断,取-1补码的后8位数 11111111
//  11111111放入c中,但我们打印的是有符号整型(%d),所以需要32个比特位,即需补上24个比特位
//  整型提升,无符号位补符号位是没有意义的,所以直接补0
//  00000000 00000000 00000000 11111111   补后的补码   符号位为0,原反补码相同
//	00000000 00000000 00000000 11111111   反码
//  00000000 00000000 00000000 11111111   原码
//  读原码为255,则输出255

Guess you like

Origin blog.csdn.net/qq_73390155/article/details/129276319