C language data storage - integer articles

foreword

In C language, understanding relative data storage is crucial for a good programmer! It is very important for programmers to gain a deeper understanding of how computers store and manipulate data through related principles. Only by deeply understanding the principles of computer storage and manipulation of data can we write more efficient and reliable programs. This article will introduce in detail how integer data is stored in C language, hoping to help readers take a step closer to programming! !

1. Introduction to data types

In C language, the basic built-in types and sizes are as follows:
insert image description here
Type meaning:

  1. Use this type to open up the size of the memory space (the size determines the scope of use)
  2. How to look at memory space perspective

1.1 Basic Classification of Types

Integer family:
insert image description here
Floating point family:

float
double
(long double)

Construction type:


数组类型
结构体类型 struct
枚举类型 enum
联合类型 union

Pointer type:

int *pi;
char *pc;
float *pf
void *pv;
……

empty type:

void means empty type (no type)
usually applied to function return type, function parameter, pointer type.

2. Storage of integers in memory

The creation of a variable is to open up space in memory. The size of the space is determined according to different types.

Next we will introduce how the data is stored in the memory that has been created!
for example:

int a = 20;
int b = -10;

We know that a/b allocates 4 bytes of space respectively, so how to store it?

Before introducing, we need to understand the following concepts.

2.1 Original code, inverse code, complement code

There are three binary representation methods for integers in computers, namely original code, complement code and complement code.
The three representation methods have two parts: the sign bit and the value bit . The sign bit uses 0 to indicate "positive" and 1 to indicate "negative".

  • The original code, complement code and complement code of positive numbers are all equal .
  • The three representations of negative integers are not equal.

Three representations of negative integers:

Original code:
The original code can be obtained by directly translating the value into binary form in the form of positive and negative numbers.

Complement code:
Keep the sign bit of the original code unchanged, and invert the other bits bit by bit to get the complement code.

Complementary code:
Complementary code is obtained by inverse code +1.

2.1.1 Why is the data stored in the memory stored in complement code

In computer systems, values ​​are always expressed and stored in two's complement. The reason is that, using the complement code, the sign bit and the value bit can be processed uniformly;
at the same time, addition and subtraction can also be processed uniformly (the CPU only has an adder). In addition, the complement code and the original code are converted to each other, and the operation process is the same. No additional hardware circuitry is required.

example:

在计算机中,数值一律用补码来表示和存储。原因在于,是用补码,可以将符号位和数值位统一处理
例如:结算1 + (-1)
 使用原码计算
00000000 00000000 00000000 00000001 --> 1的原码 
10000000 00000000 00000000 00000001 --> -1的原码 
10000000 00000000 00000000 00000020 --> 结果-2
原码计算错误

使用补码计算
 00000000 00000000 00000000 00000001 --> 1的补码
 11111111 11111111 11111111 11111111 --> -1的补码
100000000 00000000 00000000 00000000 -->结果0
补码计算正确

Tips:

  • Complementary code and original code mutual conversion operation process is the same. That is, the sign bit of the original code remains unchanged, and the other bits are inverted to the complement code; the complement code is obtained by +1. And when the original code is deduced by the complementary code, in addition to pushing the above process, the sign bit of the complementary code can also be unchanged, and the original code can be obtained at +1 after other bits are reversed bit by bit.

Let's take a look at the storage in memory again (take vs as an example):
insert image description here
we can see that for the complement of a stored in memory, we find that the order is a bit wrong!
Why is this?
This has to mention the big and small ends.

2.2 Big and small endian introduction

2.2.1 What is big and small endian?

Big-endian (storage) mode is also called big-endian storage: the data in the low-order byte of the data is stored at the high address, and the data in the high-order byte is stored at the low address.
The little-endian (storage) mode is also called little-endian storage: the data in the low-order byte of the data is stored at the low address, and the data in the high-order byte is stored at the high address.

Tips:

  • Byte order: In bytes, discuss the storage order of memory.

2.2.2 Why are there big endian and little endian?

Why is there a difference between big and small endian modes?
This is because in the computer system, we use bytes as the unit, and each address unit corresponds to a byte, and a byte is 8bit. But in C language, in addition to the 8bit char, there is also a 16bit short type , 32bit long type (depending on the compiler) and so on. In addition, for processors with more than 8 bits, such as 16-bit or 32-bit processors, since the register width is greater than one byte, there must be a problem of how to arrange multiple bytes. So it leads to big-endian storage mode and little-endian storage mode!

For example:

A 16bit short type x, the address in memory is 0x0010, and the value of x is 0x1122. Then 0x11 is the high byte and 0x22 is the low byte.
For the big-endian mode, put 0x11 in the high address, that is, 0x0011; and put 0x22 in the low address, that is, 0x0011. Little endian mode, just the opposite.

  • Our commonly used x86 and x64 structures are little-endian, while KEIL C51 is big-endian. Many ARMs and DSPs are in little-endian mode. Some ARM processors can also choose the big-endian mode or the little-endian mode by hardware.

2.2.3 A Baidu system engineer written test question

Please briefly introduce the concepts of big-endian and little-endian, and design a small program to determine the byte order of the current machine. (10 points)

Readers of related concepts can refer to 2.2.1 by themselves.

Let's analyze how to design a small program to judge the data storage mode.

As shown in the figure below, we can use the number 1 to solve the problem.
We found that if it is little-endian mode, the data stored in the first byte is 1; if it is big-endian mode, the data stored in the first byte is 0.
Then the question is transformed into: how to let the compiler only access one byte space?
In fact, it is very simple, we only need to cast the pointer of a to char*, and access it in dereference.

Code:

//代码1
判断当前机器的自己序
int check_sys()
{
    
    
	int a = 1;
	char* ret = (char*)&a;
	if (*ret == 1)
		return 1;
	else
		return 0;
}

int main()
{
    
    
	int ret = check_sys();
	if (ret == 1)
		printf("小端\n");
	else
		printf("大端\n");
	return 0;
}

//代码2,简化代码1
int check_sys()
{
    
    
	int a = 1;
	//大端返回0,小端返回1
	return *(char*)&a;
}
int main()
{
    
    
	int ret = check_sys();
	if (ret == 1)
		printf("小端\n");
	else
		printf("大端\n");
	return 0;
}

3. Ending

That's the end of this article. Next, in the C language data storage - floating point numbers, we will introduce the knowledge about floating point numbers in detail, so stay tuned. If it is helpful to you, remember to triple! thank you for your support! !

Guess you like

Origin blog.csdn.net/Zhenyu_Coder/article/details/130907969