Integer storage (detailed explanation)

Posted by wkoneal on Thu, 23 Dec 2021 08:05:37 +0100

catalogue

I Original code, inverse code, complement code

II Large and small end storage

III Verify by debugging

IV integral promotion

I Original code, inverse code, complement code

#include<stdio.h>

int main()
{
    int a = 1;
    //000000000000000 01 - original code
    //The positive original code and the negative complement are the same
    //The first binary bit at the beginning is the sign bit
    //0 is positive and 1 is negative

    return 0;
}

A is an int type, accounting for four bytes, one byte is 8 bits, so there is an integer variable, and a has 32 bits

#include<stdio.h>

int main()
{
	int a = -1;
	//1000000000000001 - original code
	//11111111111111111111111111111111111110 - inverse code
    //The sign bit of the original code does not move, and the other bits are reversed by bit
	//11111111111111111111111111111111111111 - complement
    //Inverse code plus one
	//The first is the sign bit
	//0 is positive and 1 is negative, so it is negative

	return 0;
}

The complement is stored in memory

The reason for storing complement codes is to facilitate calculation

int main()
{
	int a = 1 - 1;
	//The computer can only add, if we calculate according to the original code
	//00000000000000000000000000000001 - 1
	//10000000000000000000000000000001 - -1
	//10000000000000000000000000000010 - -2
	//The result of this calculation is - 2, which is different from what we want
	//But according to the inverse code
	//00000000000000000000000000000001 - 1
	//11111111111111111111111111111111 - -1
	//100000000000000000000000000000000 - 0 
    //There are 33 bits, while an integer has only 32 bits
	//Therefore, the last 32 bits are taken by default, and the result is 0

	return 0;
}

II Large and small end storage

Large end storage: the high bit of data is stored in the low bit of the address, and the low bit of data is stored in the high bit of the address

Small end storage: the high bit of data is stored in the high bit of the address, and the low bit of data is stored in the low bit of the address

For example, we need to save the number 0x11223344 (0x indicates that this is a hexadecimal number)

The address is arranged from low to high. Like this, the high-order data is stored in the low-order address, and the low-order data is stored in the high-order address, which is the big end storage

In this way, the high bit of data is stored in the high bit of the address, and the low bit of data is stored in the low bit of the address, which is called small end storage

So how do we check whether our computers are big end storage or small end storage

Let's design a program

&Fetch address character

*Dereference operator

#include<stdio.h>
int main()
{
	int a = 1;
	char* pa = (char*)&a;
    //The address of a will be int * by default
    //So cast the type to char * first
    //pa points to the address of the first of the four bytes of int
	if (*pa == 1)//
	{
		printf("Small end\n");
	}
	else
	{
		printf("Big end\n");
	}

	return 0;
}

The big end storage of 1 is 00 00 01

Small end storage is 01 00 00

Then * pa is the first of the four bytes of int

By judging whether the first byte is 0 or 1, you can judge whether it is large end storage or small end storage

III Verify by debugging

Step 1: press F10/F11 (Fn+F10/Fn+F11) on the keyboard to start debugging

Step 2

Then an interface will appear:

We should first change "column: automatic" to "column: 4" to facilitate our observation

Let's first look at the case when a=1: first click F10/F11 twice to store a in memory

Then take the address of a

 

The data stored in memory is stored in hexadecimal

We can see01 00 is stored in the

Because the compiler uses small end storage, the value it actually stores is 00 00 01

Restore to binary is 00000000 00000000 00000000 00000001

It's the complement of 1

Next look

a = -1

We can see that FF stored in the address of a is converted into binary, which is the complement, complement-1 is the inverse code, and the inverse code is the original code

Complement: 11111111111111111111111111

Inverse code: 11111111111111111111111110

Original code: 1000000 0000000 0000001

The first binary bit of the original code is the sign bit. If it is 1, it is negative

IV integral promotion

int main()
{
	char a = 1;
	//Assign the constant 1 to the a variable
	// =Assignment operators operate from right to left
	// 1 is a constant of int type with four bytes, while char type has only one byte
	//00000000000001 - 1 complement (positive original code and negative complement are the same)
	//00000001 - a takes the last eight bit s


	return 0;
}

Because it is stored in the small end, the first byte of int is given to a, that is, the next 8 bit s are given to a

int main()
{
	char a = 1;
	printf("%d\n", a);
	//Convert char type to int type output
	//00000001
	//Fill according to the sign bit. If the sign bit is 0, fill 24 zeros in front
	//If the sign bit is 1, fill in 24 1s before it
	//00000000000000000000000001 complement (inverse code, original code)
	char a = -1;
	printf("%d\n", a);
	//11111111
	//11111111111111111111111111111111111111 - the complement is stored in memory
    //11111111111111111111111111111111111110 - inverse code
    //1000000000000001 - original code output

	return 0;
}

Hope to help you understand plastic storage!

Every day without dancing

It's all a betrayal of life.

- Nietzsche

Topics: C Back-end