当前位置:网站首页>[C language series] - storage of deep anatomical data in memory (I) opening of summer vacation
[C language series] - storage of deep anatomical data in memory (I) opening of summer vacation
2022-07-29 05:35:00 【Gancheng なつき】
꧁ Hello, everybody ! It is a great honor to have your visit , Let's have a long way to go in programming !꧂
* Blog column :【C All living things 】*
Introduction to this article : How deep anatomical integer data is stored in memory !
Get to know the author : Aspire to be a great programmer , Xiaobai, who is currently a sophomore in College .
Inspirational terms : The tediousness of programming , Let's study together and become interesting !
Text begins
List of articles
The storage of integers in memory
Original code , Inverse code , Complement code
Introduction to byte storage mode at large and small ends
Small endian byte order storage mode
Design a program to judge the byte order storage mode of the current machine
Preface
After many nights of hard work , The final examination of the school is finally over , Next, we will enter the summer vacation , During the summer vacation , I will constantly update my knowledge points , And share it with you , First of all, the first article is about the storage of deep anatomical data in memory - The data here refers to integers , Floating point numbers will be in the next article , Share with you !
Introduction to data types
We know C In language , There are many types of data , Now let me give you some examples :

As you can see C There are so many data types in language , So let's think about a problem , What is the significance of so much data ? I think it should have these two meanings :
The meaning of type
1. Use this type to exploit the size of memory space ( Because the size determines the scope of use ).
2. How to look at the perspective of memory space !
How to understand this sentence ? Let's draw a picture to understand !

The storage of integers in memory
We open a in memory 4 Bytes of memory space , Such as :int a; that a How to allocate this 4 A space of bytes ?
Original code , Inverse code , Complement code
These three representations are composed of sign bits and numeric bits , Sign bit 0 Express positive ,1 Negative !
Original code : Directly translate the positive and negative numbers in binary form
Inverse code : The sign bit of the original code remains the same , Just reverse the other bits in turn
Complement code : Inverse code +1 It's complement
notes : A positive number , back , The complement is the same !
For plastic surgery : The data stored in the memory is actually the complement .
Why? ?
In computer system , All values are expressed and stored by complements , Because using complement , Symbol bits and value fields can be treated in a unified way .
meanwhile , Addition and subtraction can also be handled in a unified way (CPU Only adders ) Besides , Complementary code and original code are mutually transformed , The calculation cost is the same , No need for additional hardware circuits .
There are two points worth noting in this sentence :1.CPU Only adders , that 1-1 How does this algorithm work ?
Let's calculate :
It can be transformed into 1+(-1) Calculated

2. Complement code and original code are converted to each other , No need for additional hardware circuits : It means that the original code can be complemented by adding one to the inverse code , The complement can also remain the same sign bit , The other bits are inverted plus one to get the original code !
Let's take a look at the storage in memory :

Look, this is a Memory storage method , We know a=20 Of 16 The base number is represented by 0x00000014, But why is it stored upside down in memory ? Here is the problem of size end !
Introduction to byte storage mode at large and small ends
Large endian storage mode
It means that the low byte order of data is stored at the high address in memory , The high byte order of data is stored at the low address in memory .
Small endian byte order storage mode
It refers to that the high byte order of data is stored at the high address in memory , The low byte order of data is stored at the low address in memory .
Let's draw a picture to demonstrate :

Design a program to judge the byte order storage mode of the current machine
Let's suppose we open up a int a =1, Of memory , This is 4 Bytes , The form stored in memory is 0x00000001, Storing in memory is nothing more than big end and small end , As long as we take out his first byte , Judgment is not 1 You can judge , Is it big end or small end !

Let's code :
#include<stdio.h>
// Implementation function
int check_sys()
{
int a = 1;
return *(char*)& a;
}
int main()
{
int ret = check_sys();// Write a function to judge
if (ret == 1)
{
printf(" The small end \n");
}
else
printf(" Big end \n");
return 0;
}The running result is :

An exercise
What output ?
#include<stdio.h>
int main()
{
char a=-1;
signed char b=-1;
unsigned char c=-1;
printf("a=%d b=%d c=%d",a,b,c);
}We know char It generally refers to symbolic char, It mainly depends on the compiler , The signed char The value range is : -128~127
Unsigned char The value range is :0~255 How to calculate , No, you can follow bloggers , Bloggers will tell you by heart , It's just more here , There should be some content about this in the title !
char Yes, it can be stored in -1 Of , So let's draw a picture to explain it in detail :

So the output is -1,-1,255 La !
Finally, this blog sharing is over here , I wish you all a happy summer vacation !
边栏推荐
猜你喜欢
随机推荐
The function of using wechat applet to scan code to log in to the PC web of the system
【C语言系列】—三种方法模拟实现strlen库函数的方法
浅谈范式
Day 3
【C语言系列】— 不创造第三个变量,实现两个数的交换
ClickHouse学习(十)监控运行指标
Pyqt5: Chapter 1, Section 1: creating a user interface using QT components - Introduction
Database operation day 6
Realize simple database query (incomplete)
Redirection and files
阿里云张新涛:异构计算为数字经济提供澎湃动力
B - identify floating point constant problems
Solution: find the position of the first and last element in a sorted array (personal notes)
分配内存:malloc()和free()
Day 2
Basic use of redis
·来一篇编程之路的自我介绍吧·
C language first level pointer
C language one-dimensional array
携手数字人、数字空间、XR平台,阿里云与伙伴共同建设“新视界”









