当前位置:网站首页>[Zhang San learns C language] - deeply understand data storage
[Zhang San learns C language] - deeply understand data storage
2022-07-02 06:47:00 【Xiexishan】
【 Zhang Sanxue C Language 】— Deep understanding of data storage
One , data type
Different data types represent the size of space opened up by different data in memory
eg:int In memory 4 Bytes
| whole type | Content Be careful : The contents in brackets are the number of bytes occupied by each type |
|---|---|
| data type | char(1 byte ) short(2 byte ) int(4 byte ) long(8 byte ) |
| floating-point | float (8 byte ) double(16 byte ) |
| Construction type | An array type ( The memory size is decided by yourself ) Structure struct ( Observe memory alignment ) enumeration enum( Memory elements determine ) Joint type union( The largest element ) |
| Pointer types | void Indicates the type , Usually used for the return type of a function , The parameters of the function , Pointer types |
C Language has no string type , So use arrays to store strings eg:char[10]="abcdef"
Two . The storage of integer data in memory
1. Source code , Inverse code , Complement code
The symbols in the computer have been stored in memory in binary form
Data is stored in memory in the form of complement
The first is the sign bit , The remaining bits are data bits
0 Being positive ,1 Negative
Source code : Direct binary can be translated into binary in the form of positive and negative numbers
Inverse code : The sign bits remain the same , The data position is reversed
Complement code : Inverse code +1
Positive numbers : Source code = Inverse code = Complement code
negative : Store... As a complement
eg:
Positive numbers
date:10
bin:00000000 00000000 00000000 00001010
Source code = Inverse code = Complement code =00000000 00000000 00000000 00001010
negative
date:-10
bin: 10000000 00000000 00000000 00001010
Inverse code :11111111 11111111 11111111 11110101
Complement code :11111111 11111111 11111111 11110110
2. The reason why data is stored in the form of complement in memory
a. The use of complement can unify the symbol bit and data field
b.CPU Only adders , Using complement can make addition and subtraction unified
3, data storage
Byte order :
Concept :cpu The order in which data in memory is accessed in bytes
Because there are high and low addresses in memory , There are high and low binary data
The host byte order mainly depends on cpu framework : X86- The small end ; MIPS- Big end
Byte order is mainly used in network communication , Communication between different hosts , If the byte order is different, it will cause data ambiguity
classification :int=0x11223344
Big end storage : The low address stores the high address Storage in memory 11 22 33 44
Small end storage : The low address stores the low address Storage in memory 44 33 22 11
notes : The size end is a high-frequency examination site in interviews and examinations , To learn more, click the link below to learn .
3、 ... and . The storage of floating-point data in memory
1,IEEE754 The standard document stipulates , The representation of any floating-point number in memory is divided into three parts
| Symbol | Memory occupied by each bit of single precision floating-point numbers | Memory occupied by each bit of double precision floating-point numbers |
|---|---|---|
| s— Sign bit | 1 A bit | 1 A bit |
| E— Exponential position | 8 A bit | 11 A bit |
| M— mantissa | 23 A bit | 52 A bit |
Floating point numbers are expressed in exponential form in the computer , Decompose into : Numeral character , mantissa , Exponential sign , Index Four parts .
The number symbol accounts for 1 Bit binary , The positive and negative of a number .0 Express positive ,1 Negative
Exponential operators account for 1 Bit binary , The positive and negative of the index .
The mantissa represents the significant number of floating-point numbers ,0.xxxxxxx, But don't save the beginning 0 Sum point .
The effective number of the index .
How many places does the index occupy , How many bits does the mantissa occupy , It's up to the computer system to decide .

Two special cases :
E For all 0: At this time, the real index value is 1-127=-126,M Represent true value , Usually used to indicate positive and negative 0
E For all 1: At this time, the real index value is 255-127=128,M For all 0 Hour means positive and negative infinity
2. Value range
| type | Number of bits | Significant figures | Range of values |
|---|---|---|---|
| float | 32 | 6-7 | -3.410(-38)~3.410(38) |
| double | 64 | 15-16 | -1.710(-308)~1.710(308) |
| long double | 128 | 18-19 | -1.210(-4932)~1.210(4932) |
If storage is more important than accuracy , Consider using float type . contrary , If accuracy is the most important condition , Then use double type .
Floating point variables can be promoted to larger Radix types ( from float Type to double type ). When you perform arithmetic on floating-point variables , Usually there will be an improvement . This arithmetic is always performed with the same precision as the variable with the highest precision .
float f_short;
double f_long;
long double f_longer;
gf_short = f_short * f_long
In the previous example , Variable f_short Promote to type double And with f_long Multiply ; then , Result rounded to type float, Then give to f_short.
3. Application example analysis
stay C and C++ in , The following assignment statement :
float a=0.1;
Compiler error :warning C4305: ‘initializing’ : truncation from 'const double ’ to 'float ’
reason : stay C/C++ in , To the right of the equal sign 0.1, Think it's a float, But the compiler thinks it's a double( Because decimals default to double), So I have to report this warning, It's usually changed to 0.1f It's all right. .
Welcome to exchange , If it works , Remember to comment + Collection + Pay attention to ! meow ~~
Today's rubbish : I didn't understand until I grew up , Nothing is difficult in the world , I'm just afraid that those who have a mind .
边栏推荐
猜你喜欢

Latex 报错 LaTeX Error: The font size command \normalsize is not defined问题解决

Win10网络图标消失,网络图标变成灰色,打开网络设置闪退等问题解决

Sublime Text 配置php编译环境

js中map和forEach的用法

Latex 编译报错 I found no \bibstyle & \bibdata & \citation command

pytest(1) 用例收集规则

Fe - wechat applet - Bluetooth ble development research and use

Sentry搭建和使用

Blog directory of zzq -- updated on 20210601

Win10:添加或者删除开机启动项,在开机启动项中添加在用户自定义的启动文件
随机推荐
Linux MySQL 5.6.51 Community Generic 安装教程
Eggjs -typeorm treeenity practice
FE - Eggjs 结合 Typeorm 出现连接不了数据库
20210306转载如何使TextEdit有背景图片
微信小程序基础
selenium备忘录:selenium\webdriver\remote\remote_connection.py:374: ResourceWarning: unclosed<xxxx>解决办法
There is no way to drag the win10 desktop icon (you can select it, open it, delete it, create it, etc., but you can't drag it)
Latex compilation error I found no \bibstyle &\bibdata &\citation command
Function execution space specifier in CUDA
Sublime Text 配置php编译环境
ZZQ的博客目录--更新于20210601
如何调试微信内置浏览器应用(企业号、公众号、订阅号)
Sentinel rules persist to Nacos
Build learning tensorflow
由於不正常斷電導致的unexpected inconsistency;RUN fsck MANUALLY問題已解决
Sentinel Alibaba open source traffic protection component
Loops in tensorrt
NodeJs - Express 中间件修改 Header: TypeError [ERR_INVALID_CHAR]: Invalid character in header content
Warp matrix functions in CUDA
ctf-web之练习赛