当前位置:网站首页>[advanced C language] integer storage in memory
[advanced C language] integer storage in memory
2022-06-11 21:13:00 【Super good baldness】
This series , We will explore C The deeper content of language !
Super good product
List of articles
Preface
In the daily process of typing code , We often use integer constants to assign values to variables , But we may not have considered how different variables are stored in memory ! Today I'd like to explore the mystery with you .
The internal capacity may be slightly larger , We can't finish it. We can collect it , Then look at !
One 、 What integers include ?
1. Classification of integers
Integers include char,short,int,long,long long. Of course, someone might ask , Why? char It's also an integer ?
That's because char Every character has its corresponding ASCII Code value , So of course it's an integer ! This is the integer family !
2. Integers store in memory the basics you must know ( Original inverse complement )
In memory , Whether it's positive or negative , All storage forms are stored in the form of complement !!
First , When stored in memory , We all store binary integers . Then use binary to represent the original code of integer , Inverse code and complement code !
There are three kinds of integers in the computer 2 Decimal representation , The original code 、 Inverse and complement .
The three representations have two parts: sign bit and numeric bit , The sign bits are all used 0 Express “ just ”, use 1 Express “ negative ”.
A positive number 、 back 、 The complement is the same .
The three representations of negative integers are different
Original code
The original code can be obtained by directly translating the numerical value into binary in the form of positive and negative numbers .
Inverse code
Change the sign bit of the original code , The reverse code can be obtained by inverting other bits in turn .
Complement code
Inverse code +1 You get the complement .
Illustrate with examples :int a= -20;( The first bit is the sign bit )
Original code :10000000 00000000 00000000 00010100
Inverse code :11111111 11111111 11111111 11101011
Complement code :11111111 11111111 11111111 11101111
Complement is the way to store in memory !!
3. Classification of integers
Integer types can be divided into signed and unsigned types ! That is to say signed and unsigned.
signed and unsigned The difference is that Sign bit Remember or not Value bits !
In general ,int,char An equal integer is usually a signed number .
Illustrate with examples :
unsigned int a= -20;
An unsigned number , Treat the first sign bit as a numeric bit , So this is also a positive number . The former is the same as the latter
Original code :10000000 00000000 00000000 00010100
Inverse code :10000000 00000000 00000000 00010100
Complement code :10000000 00000000 00000000 00010100
Two 、 Specific storage methods
1. Large and small end
We can find out , When stored in memory ,int a and int b Shouldn't it be stored with binary complement ? Should not be 32 Bit storage form of binary bits ?
Next , We explore !

0x yes 16 Hexadecimal representation , So we guess , Probably stored in binary complement , With 16 The system is displayed , So give it a try !
int b=20;
The former is the same as the latter :0000 0000 0000 0000 0000 0000 0001 0100
( Binary number every four bits , It's a bit. 16 Hexadecimal number )
16 Hexadecimal said :00 00 00 14
A surprise discovery , It's the same !!!!!
But there are still some differences , The order is different !
So that leads to what we are going to talk about today ( Large and small end ( Byte order )) That is, big end and small end

Big end : The low bit of the data is saved to the high address in the memory , The high bit of the data is saved to the low address of the memory
Short paragraph : The low order of the data is saved to the low address in the memory , The high bit of the data is saved to the high address of the memory
therefore , After understanding and mastering , Try the compiler you are using for storage !
Like blogger I use vs2022, Then let me have a try !!
2.unsigned and signed The difference between integer classes

Let's start with a question , Explore through the topic !
char a= -1;
Original code :10000000 00000000 00000000 00000001
Inverse code :111111111 11111111 11111111 11111110
Complement code :11111111 11111111 11111111 11111111( In memory storage mode : Complement code )
however char Take up a byte
11111111
To use %d Output , Then it will involve integer promotion .
Rules for integer Promotion : And lower than its own type or some operation , Integer promotion will occur
1.signed type : When a positive number increases , repair 1; Negative numbers make up 0;( Complement as the highest sign bit )
2.unsigned type : In ascension , Direct compensation 0;
that ,char a= -1, With %d When the output , When integer lifting ,
11111111
Aftertreatment :11111111 11111111 11111111 11111111 Stored in memory as complement , Output in original code
Original code :10000000 00000000 00000000 00000001
( Complement to original , It is still the first to take the opposite ( The sign bits remain the same !), One more )
So the end result is -1
signed char a = -1 And char a = -1 It's the same .
In many environments ,char,int Are signed numbers !
So it turns out that -1
unsigned char a = -1
Original code :10000000 00000000 00000000 00000001
Inverse code :111111111 11111111 11111111 11111110
Complement code :11111111 11111111 11111111 11111111
Take only the last byte
11111111
Improve the overall shape ( Unsigned numbers are added when they are raised 0 ):00000000 00000000 00000000 11111111( Memory to make up Code storage )
The highest bit is 0, Positive number , Both positive and negative complements are the same , So the result is 255.
Let's see the results

I believe you already know , And familiar with it , Of course, more practice is needed !
Strike while the iron is hot !

(%u Output for unsigned )
primary 10000000 00000000 00000000 10000000
back 11111111 11111111 11111111 01111111
repair 11111111 11111111 11111111 10000000
char 10000000
Improve the overall shape 11111111 11111111 11111111 10000000
result :4294967168

answer :-10
Is the result right ?
summary
1. In memory , Whether it's positive or negative , All storage forms are stored in the form of complement !!
2. In general ,int,char An equal integer is usually a signed number .
3. Large and small end
Big end : The low bit of the data is saved to the high address in the memory , The high bit of the data is saved to the low address of the memory
Short paragraph : The low order of the data is saved to the low address in the memory , The high bit of the data is saved to the high address of the memory
Don't forget to think about it !
边栏推荐
- How to Load Data from CSV (Data Preparation Part)
- JVM之堆区
- 为什么100G网络传输要使用iWARP、RoCE v2、NVMe-oF等协议
- BUG -- coredump使用
- Tensorflow 2. X Getting Started tutorial
- Serval and Rooted Tree(CF1153D)-DP
- JVM runtime constant pool and direct memory
- 新品发布:LR-LINK联瑞推出首款25G OCP 3.0 网卡
- Which Bluetooth headset is better within 500? Inventory of gifts for girls' Day
- [Monday commuter radio station] cron expression. It's enough to read this article
猜你喜欢

修改本地微信小程序的AppID

Teach you how to use win7 system to quickly build your own website

Live broadcast with practice | 30 minutes to build WordPress website with Alibaba cloud container service and container network file system

Tensorflow 2. X Getting Started tutorial

全球机器视觉市场规模持续上涨,PoE图像采集卡为工业相机提供高速传输通道

【C语言进阶】整型在内存中的存储

Pyqt5 technical part - set the default value of qcombobox drop-down box and get the current selection of the drop-down box
![[nk] 牛客练习赛100 C 小红的删数字](/img/f1/a99600e1800c087aceb60a559dee39.png)
[nk] 牛客练习赛100 C 小红的删数字

Role of RESNET residual block

【数据可视化】使用 Apache Superset 可视化 ClickHouse 数据
随机推荐
从概率论基础出发推导卡尔曼滤波
Serval and Rooted Tree(CF1153D)-DP
IDEA中,运行yarn命令,显示无法加载文件,因为在此系统上禁用运行脚本
Goland中在文件模板中为go文件添加个人声明
成长的12条黄金法则
[index system] the latest modeling method of data warehouse index system
Teach you how to use win7 system to quickly build your own website
LR-LINK联瑞携新品首次亮相数博会-助力新基建数据中心建设
RANSAC extract cylinder (matlab built-in function)
Chinese text classification based on CNN
JSON introduction
The difference between VaR and let_ The difference between let and VaR
Tensorflow 2. X Getting Started tutorial
为什么100G网络传输要使用iWARP、RoCE v2、NVMe-oF等协议
修改本地微信小程序的AppID
Three waves of changes in cloud computing
Add personal statement for go file in file template in Golan
Notes on the preload method of Gorm
Why microservices are needed
vectorDrawable使用报错
