当前位置:网站首页>Storage of C language integer in memory
Storage of C language integer in memory
2022-06-21 08:34:00 【With special education】
Data in memory
C What are the data types

Explain it separately void type 
The meaning of data type
1. Use this type to exploit the size of memory space ( Size determines the range of use ).
2. How to look at the perspective of memory space .
Understand the basic knowledge of integer storage in memory
1. The data in memory is in the form of 2 Base store
2. There are three ways to express integers in a computer , The original code 、 Inverse and complement
But in essence, integers are stored as complements
3. The first bit represents the sign bit , The sign bit uses 0 Express “ just ”, use 1 Express “ negative ”
Positive integer : Original code 、 Inverse code 、 The complement is the same
Negtive integer : Original code 、 Inverse code 、 Complements are not the same
Original code : The original code is written directly according to the binary of the number
Inverse code : The sign bits remain the same , The reverse of other bits is the reverse code
Complement code : Inverse code +1 It's complement
The order of data storage in memory
Large byte storage : The lower bits of the data are stored at the higher address , The high bit of data is stored at the low address .
Small end byte storage : The lower bits of the data are stored at the lower address , The high bit of data is stored at the high address .
Example :1 Store on the large and small sides of memory
int a=1 0x00 00 00 01 -16 Base number
If it is The small end Then it is stored in memory in this way 01 00 00 00 Big end 00 00 00 01
Be careful :VS2019 Using small end storage
Design a program to determine whether the current compiler is large-end storage or small-end storage
#include <stdio.h>
int check_sys()
{
int i = 1;
return (*(char *)&i);
}
int main()
{
int ret = check_sys();
if(ret == 1)
{
printf(" The small end \n");
}
else
{
printf(" Big end \n");
}
return 0;
}
principle : Actually used char Step size for type access , because char Access only one byte at a time , We just need to judge the value of the first byte If the value is 1 The explanation is small end , by 0 Description is big end .

Program exercises
#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);
return 0;
}
char a= -1 ( Save complement )
Original code :10000000000000000000000000000001
Inverse code :1111111111111111111111111111111111110
Complement code :1111111111111111111111111111111111111
And because it's char Type can only be saved 8 individual bit So the truncated storage is 11111111
To press %d The printing should be improved and the original code should be printed because char It is a signed number, and the lifting complement is the original sign bit
1111111111111111111111111111111111111 - Complement code
1111111111111111111111111111111111110 - Inverse code
10000000000000000000000000000001 - Original code
So the answer is -1 ,char b Is the same
unsigned char c=-1
What you save is 11111111
To press %d The printing should be improved and the original code should be printed because char It is an unsigned number that is raised and complemented by 0
0000000000000000000000011111111 - Complement code - Complement code - Original code The positive integer is the same as the original inverse complement
The answer is 255
#include <stdio.h>
int main()
{
char a = -128;
printf("%u\n",a);
return 0;
}
a=-128
10000000000000000000000010000000 - Original code
111111111111111111111111111101111111 - Inverse code
11111111111111111111111111111000000 - Complement code
the reason being that char a Deposit is 10000000
%u Print unsigned print is a complement
Plastic improvement is
11111111111111111111111110000000 - Complement code ( At this point, the highest bit is not the sign bit )
So the print is 4294967168
#include <stdio.h>
int main()
{
char a = 128;
printf("%u\n",a);
return 0;
}
a=128
00000000000000000000000010000000 - Original code
011111111111111111111111111101111111 - Inverse code
01111111111111111111111111111000000 - Complement code
char a=10000000
Plastic surgery improves
11111111111111111111111111110000000 - Complement code
Because it is printed as an unsigned number, the first sign is not the same as the original reverse complement of the sign bit
What is printed is still 4294967168
int i= -20;
unsigned int j = 10;
printf("%d\n", i+j);
// Operate in the form of complement , Finally, it is formatted as a signed integer
i=-20
111111111111111111111111111111101100 - Complement code
j=10
00000000000000000000000000001010 - The original is the same as the reverse
Addition is the addition of complements
i+j
111111111111111111111111111111110110 - Complement code
Press %d The original code is required for printing
111111111111111111111111111111110101 - Inverse code
10000000000000000000000000001010 - Original code
So the print is -10

Here I borrow the picture of big brother Lin slowly 

answer 128+127=255
7.
#include <stdio.h>
unsigned char i = 0;
int main()
{
for(i = 0;i<=255;i++)
{
printf("hello world\n");
}
return 0;
}
Because unsigned numbers are always less than or equal to 255 Dead cycle
Please forgive me for your poor writing !
边栏推荐
- Nodejs post request JSON type and form type
- Joking Domain Driven Design (VI) -- Boundary context -- Design
- STL教程2-MyArray框架实现
- STL tutorial 3- type conversion static_ cast、dynamic_ cast、const_ cast、reinterpret_ Cast method
- Unity 5 自帶的Mono也可以支持C# 6
- Showctf web primer series
- JUnit中的@Transactional消失不见,@Rollback是否能单抗测试回滚的大旗?
- 【C】【时间操作】C语言中关于时间的操作
- Gql+nodejs+mysql database
- 2022-2028 global hydrogen engine industry research and trend analysis report
猜你喜欢

Mono fourni avec l'unit é 5 peut également supporter C # 6

日记(C语言总结)

2022-2028 global section valve industry research and trend analysis report

Eureka's timedsupersortask class (periodic task with automatic interval adjustment)

How to build a deep learning framework?

Two image enhancement methods: image point operation and image graying

showCTF Web入门题系列

Introduction to testing - Software Test Model

移动应用开发总结

Summary of problems and errors encountered in tidb4.0.0 (tiup deployment)
随机推荐
[DB written interview 220] how to back up control files in oracle? What are the ways to back up control files?
leetcode:打印两个有序链表的公共部分
PHP类与对象详细介绍
Doc common syntax, updating
日記(C語言總結)
AQS source code exploration_ 01 handwriting a simplified reentrantlock reentrant lock
Showctf web primer series
Fd: file descriptor
Interview experience - bytes
Showctf starter file contains series
Topic34——31. 下一个排列
Mono of unity 5 can also support C # 6
给两个字符串s和t,判断t是否为s的重新排列后组成的单词
Post process basic notes (important items)
STL教程3-类型转换static_cast、dynamic_cast、const_cast、reinterpret_cast方法
Unity Detailed explanation of meta file function
4.9 commander. js
How to write attractive titles for short videos? Learn these tips to make your title more convincing
Summary of problems and errors encountered in tidb4.0.0 (tiup deployment)
【MGT】代码解读之model-MGT