当前位置:网站首页>[C language] data type storage, original code, inverse code and complement code
[C language] data type storage, original code, inverse code and complement code
2022-06-11 23:22:00 【InfoQ】
The meaning of type :
- It is this type that determines the size of its memory space once it is determined , It also determines how big its scope is . For example, it's like : When you put a variable a The value of is assigned to int plastic , So its storage size is 4 The range of bytes is -32768~32767.
- Be careful , Various types of storage sizes are related to the number of system bits , But the current 64 Bit system is the main .
C The types of languages are divided into
- Memory is a major component of a computer , It is used to save the program and data when the process is running , Also called executable memory . In the computer , Memory space generally refers to the main memory space ( Physical address space ) Or the system allocates memory space for a user program . Methods to expand memory space generally include increasing memory size and virtual memory .
Construction type
- An array type :Why is an array type a construction type ? Because of the assumptionint arr[20], So it's an array type, isn't it . that int [20] It's the type , Then I'll make another change int [10] This type is changing So what if you're a different type? Character type , Floating point type like this , Therefore, array is also a custom type and a construction type .
- Type of structure :struct,Member types are changing , Then its structure is also changing . If you don't know the structure, you can see the content of this structure in my article .
- Enumeration type :enum,This is C A key word of language , At that time, I will take out and write an article to focus on the enumeration type in C What role does language play .
- Consortium type :union, This is also called a community , We won't talk about this now .
Pointer types
- The purpose of a pointer is actually to store an address assigned to a variable in it , And the bytes of the pointer are 4 Bytes .
- In this way, we use a piece of code to let you intuitively see if the pointer is 4 Bytes , Because the blogger told you about the content of the wild pointer, but the content of the pointer has not been told you yet ,At that time, bloggers will also write an article about pointers ,Thus, pointer type is a special type . Pay attention to the , The pointer is defined with *
#include<stdio.h>
int main(void)
{
int a = 10;
int* p = &a; // Shaping pointer variables p receive a The address of
// Here, all data types are defined as pointers -- 32 position 4byte 64 position 8byte
printf("%d\n", sizeof(int*));
printf("%d\n", sizeof(short*));
printf("%d\n", sizeof(double*));
printf("%d\n", sizeof(char*));
}- Running results :

Empty type
- A special return type , Represents an empty function , That is, a function without a return value . Empty type ( No type ) void Used to show that a function does not return any value . It can also point to void Pointer to type , After explanation , This pointer can point to various types of data objects
- void There is no reference , When you add no parameters when defining a program , Although the program will run . But there will be a waring Hints are unnecessary parameters
The return type of the function
#include<stdio.h>
void print()
{
printf("hello word\n");
}
int main(void)
{
print();
}The parameters of the function
#include<stdio.h>
void print()// It is also possible to transfer parameters without parameters , But not here, so we C Language does not receive
{
printf("hello word\n");
}
int main(void)
{
print(10);// stay print Give arguments to the function 10
}Shaping the storage space in memory
#include<stdio.h>
int main(void)
{
int a = 5;
int b = -3;
}

- Did you find out , The storage results of these two are different , Next, I'll tell you how plastic is stored in memory .
- In fact, it is necessary to understand the original code first , Inverse code , And the concept of complement .
- In the computerSigned number ( plastic )There are three ways to express it, namely : Original code , Inverse and complement . The three representations have two parts: sign bit and numeric bit ,The sign bit is used 0 Representation bit " just ", use 1 Expressed as " negative ",The three representations of numerical bits are different . notes : Unsigned source code, inverse code and complement code are the same .
- There are two kinds of signed numbers. One is called positive number , The other is called a negative number . It's the same in positive numbers , However, in negative numbers, these three expressions are different
- Above are three cases of negative numbers ,........... This is omitted if it's a plastic front 32 Digit number , Note that the highest position here is 1, So there are three forms of negative numbers .
Original code
- Directly convert the number into binary according to the positive or negative form
int a = 5;//4 Bytes —— 32bit position
//0( The highest positive number )0000000000000000000000000000101
int b = -3;
//1( The highest negative number )1111111111111111111111111111101
Inverse code
Complement code
Why is the complement stored in the computer ?
Introduction to big end and small end :
- Big end : Storage mode meansMemoryThe low order in is saved inMemoryIn the middle ofHigh addressamong , And the high end of the data , Save inMemoryOfLow addressamong .
- The small end : Storage mode meansdataThe low order in is saved inMemoryMiddle low address , And the high end of the data , Save inMemoryOfLow addressamong .
- Memory spaces are numbered , We all call the low bit of memory low address , If the number is large, it is called high address , Data can be stored in any way , But when you go back to your program, how must you store it? For example, what you shape and store is 11 22 44 33 ( Binary conversion hex 1 Bytes =4 individual bit position ), Then you also need to shape and store it in this way 11 22 44 33 ! But we don't usually store memory like this . There are usually two storage methods. One is calledBig endAnother storage mode isThe small endThe storage mode of .
- The assumption is :0x11223344, This 44 It's low , and 11 It's the high position , Then the number of high addresses to be saved to memory , The high bit of the data is stored at the low address —— Big end
- Suppose again :0x44332211, This 44 It's low , and 11 It's the high address , Low byte data is stored at a low address in memory , The high byte data is stored at the high address —— The small end
Judge the big end and the small end
- int a = 1;
- The small end :0x01 00 00 00
- Big end :0x00 00 00 01
Code demonstration
#include<stdio.h>
int main(void)
{
int a = 1;
char *p = (char*)&a;
if (*p == 1)
{
printf(" The small end \n");
}
else
{
printf(" Big end \n");
}
return 0;
}Custom function code
#include<stdio.h>
int Testing()
{
int a = 1;
char * pb = (char*)&a;
if (*pb == 1)
return 1;
else
return 0;
}
int main(void)
{
int ret = Testing();
// return 1 Represents the small end , return 0 It represents big end
if (ret == 1)
{
printf(" The small end \n");
}
else
{
printf(" Big end \n");
}
return 0;
}- The pointer type determines how many bytes the pointer dereference accesses , such as :charp; So herep Only one byte can be accessed , According to the data type you define
- The pointer type determines the pointer +1,-1 It's a few bytes , such as :char * p + 1; So what is skipped is a byte Judge according to the data type int Namely 4 byte
- Be careful : The integer lifting complement is the highest sign bit ,'0' Being positive ,'1' Negative
The storage of floating point in memory
- 3.14 、1E10( This is actually 1.010 Of 10 Power —E)
- C Floating point types in languages are float、double and long double type .
- The first 1 Columns are general notation ;
- The first 2 Column is scientific notation ;
- The first 3 The column is exponential notation ( Also known as e Notation );
- This is how scientific notation is written in computers ,e The following numbers represent 10 The index of ;
#include <stdio.h>
int main(void)
{
int n = 9;
float *pFloat = (float *)&n;
printf("*pFloat The value of is :%d\n",n);
printf("*pFloat The value of is :%lf\n", *pFloat);
*pFloat = 9.0;
printf("num Value :%d\n", n);
printf("*pFloat The value of is :%lf\n",*pFloat);
return 0;
}- Running results :
- As can be seen from the above code : Floating point and integer memory are stored in different forms !
- Some friends may have some questions , Why does the second line run the result *pFloat The answer is :0.000000, And the third line num The result of the operation is :1091567616, I think it should be 9.000000 and 9 ah . good , Don't worry. Now let's talk about why !
- Print :0.000000 Because : If you take it back in the form of a floating point number when you put it in the form of a plastic number , Not getting the result we expected , Is it possible to explain how to store the result of integer and floating-point typeIt's different, If it's the same, it'sThe same result
- Print :1091567616 Because :*pFloat We put it in the form of floating point numbers , And then to n The positive form of is taken as an integer ,Took out a wrong value
- This explains the difference between the storage method of shaping and the storage method of floating-point type
- Floating point binary complement
- for example :7.0 — 0111 — 0111.0( Give... After the decimal point 0 That's all right. ) This is it. 7.0 Binary representation of
- Any binary floating point number V It can be expressed in the following form :
- (-1)^ M * 2 ^ E
- (-1)^ s The sign bit , Whens = 0 When ,V = Positive numbers(except 0 outside , Any number of 0 The second power equals 1) Whens=1 When ,V = negative
- M It represents a significant number , Greater than or equal to 1, Less than 2
- 2 ^ E Indicates the index bit
Storage mode diagram :


- The following example : Floating point numbers 9.0
- 1001
- 1001.0
- 1.001 * 2 ^ 3 —— Be careful : This is binary
- (-1) ^0*1.001* 2 ^3 = (-1)^ M * 2 ^ E
- 0 10000010 00100000000000000000000 —— E = 3 + 127 = 130
- The numbers we mentioned above are printed directly as the original code1091567616
- In this way, we can be sure that — s = 0 ,M = 1.001 ,E = 3 , Be careful M = 1.001 It's actually :0001
- The following example : Floating point numbers 0.5
- 0.5
- 0.1 — Be careful : This is one of the binary -1 The first digit after the decimal point , yes 2 The negative power of is 0.5
- 1.0 * 2 ^ -1 —— Convert to scientific counting
- (-1 ^0) *1.0* 2 ^-1—— Be careful : Index E Viewed as an unsigned number
- s = 0,M = 1.0,E = -1
- For eight E It is , The middle number is :127, For eleven E It is , The middle number is 1023,In fact, if your E If it's negative, take -1 Let's give you an example Eight is :-1+127 = 126,11 It's about :-1+1023 = 1022
- ****E Incomplete 0 Not all 1
- Floating point numbers 5.5
- 101.1 // The front is 101 according to 8421 Code to , The back is 2 Negative power of be equal to 0.5
- (-1) ^ 0 * 1.001 * 2 ^ 2
- s = 0 ,M = 1.011 ,E = 2 (E+127=129)
- 0100 0000 1011 0000 0000 0000 0000 0000 —— here E The storage of is 129,E The value of the or 129-127=2, If you are double So here we have to subtract 1023 To the real E Value
- 0x40B00000 —— Converted binary hexadecimal digits
- The result of debugging memory :
-
- E All for 0 ( Understanding can )
- E All for 1 ( Understanding can )
边栏推荐
- [Day2 intensive literature reading] time in the mind: using space to think about time
- Tensorflow [actual Google deep learning framework] uses HDF5 to process large data sets with tflearn
- Data visualization platform based on template configuration
- Live broadcast preview | featurestore meetup V3 is coming!
- 【Day11-12 文献精读】On magnitudes in memory: An internal clock account of space-time interaction
- Research Report on development trend and competitive strategy of global wafer recycling and OEM service industry
- PHP mkdir(): permission denied uploading a file will change the folder permission to 411 permission
- 双向带头循环链表(C语言)
- [day13-14 intensive literature reading] cross dimensional magnetic interactions arise from memory interference
- SDNU_ ACM_ ICPC_ 2022_ Weekly_ Practice_ 1st (supplementary question)
猜你喜欢

2022年高处安装、维护、拆除操作证考试题库模拟考试平台操作

Altium designer工程下多个原理图和PCB图的一一对应

帝国理工等最新《胶囊网络综述》论文,29页pdf阐述胶囊的概念、方法与应用

Games-101 闫令琪 5-6讲 光栅化处理 (笔记整理)

One to one correspondence of multiple schematic diagrams and PCB diagrams under Altium designer project

04 automatic learning rate - learning notes - lihongyi's in-depth learning 2021

【自然语言处理】【多模态】ALBEF:基于动量蒸馏的视觉语言表示学习

How to do investment analysis in the real estate industry? This article tells you

Application of Lora wireless communication module Lora technology in smart home light control

小程序启动性能优化实践
随机推荐
Introduction to Solr Basics
【Day2 文献精读】Time in the mind: Using space to think about time
抗原产品进入家庭,中国医疗器械企业迎来新蓝海
The second bullet of in-depth dialogue with the container service ack distribution: how to build a hybrid cloud unified network plane with the help of hybridnet
Jetpack architecture component learning (3) -- activity results API usage
Share a treasure website necessary for new media operation for free
2022年安全员-A证考题模拟考试平台操作
Jetpack架构组件学习(3)——Activity Results API使用
Data processing and visualization of machine learning [iris data classification | feature attribute comparison]
[Day9 literature extensive reading] on the (a) symmetry between the perception of time and space in large-scale environments
2022年R1快开门式压力容器操作考题及在线模拟考试
移印工艺流程及应用注意事项
[day11-12 intensive literature reading] on languages in memory: an internal clock account of space-time interaction
Tensorflow [actual Google deep learning framework] uses HDF5 to process large data sets with tflearn
04 automatic learning rate - learning notes - lihongyi's in-depth learning 2021
GMN of AI medicine article interpretation
[day1/5 literature intensive reading] speed constancy or only slowness: what drives the kappa effect
How to make scripts executable anywhere
2022 low voltage electrician certificate and online simulation examination
华为云、OBS、

