当前位置:网站首页>Seven examples to understand the storage rules of shaped data on each bit
Seven examples to understand the storage rules of shaped data on each bit
2022-07-04 10:25:00 【sqjddb】
In order to understand these seven examples smoothly , The following must be clear :
1. The shaping data is stored in the form of complement ,C The integer of language includes char,short, int,long
2. Improve the overall shape :
In the process of arithmetic operations involving integer numbers , All integer numbers will be converted to integer form ( It's usually int). If int Cannot accommodate all values of the original data type , Then it will be converted into unsigned int To deal with it
3. Rules for integer Promotion :
If it's a signed number , In the front 8*3 Bit complement sign bit
If it's an unsigned number , Then the front face 8*3 Place complement 0
4. use char Type can store integer range
therefore char The range of integers that can be stored by type is -128~127
Look at these examples after understanding
①
// 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);
return 0;
}
Running results :
-1 Of
Original code :10000000 00000000 00000000 00000001
Inverse code :11111111 11111111 11111111 11111110
Complement code :11111111 11111111 11111111 11111111
char Type can only be stored low 8 A bit 11111111
The title is printed with plastic , There's an integer boost , According to integer promotion rules
a,b All promoted to 11111111 11111111 11111111 11111111
and c Upgrade to 00000000 00000000 00000000 11111111
These are complements , The corresponding is -1 -1 255
②
#include <stdio.h>
int main()
{
char a = -128;
printf("%u\n",a);
return 0;
}
③
#include <stdio.h>
int main()
{
char a = 128;
printf("%u\n",a);
return 0; }
②③ The running results of are all the figures in the figure below
-128 Of
Original code :10000000 00000000 00000000 10000000
Inverse code : 111111111 11111111 11111111 01111111
Complement code :111111111 11111111 11111111 10000000
128 Of
Original code :00000000 00000000 00000000 10000000
Inverse code :011111111 11111111 11111111 01111111
Complement code :011111111 11111111 11111111 10000000
low 8 individual bit Bits are stored in char in 10000000
Print as unsigned integer , Integer raised to 00000000 00000000 00000000 10000000
The data corresponding to this complement is the data in the above figure
④
#include <stdio.h>
int main()
{
int i = -20;
unsigned int j = 10;
printf("%d\n", i + j);
}
-20 Of
Original code :10000000 00000000 00000000 00010100
Inverse code :11111111 11111111 11111111 11101011
Complement code :11111111 11111111 11111111 11101100
10 Of
Original inverse complement :00000000 00000000 00000000 00001010
Add the two complements to get
11111111 11111111 11111111 11110110
Print in integer
Subtract one from the above complement :
11111111 11111111 11111111 11110101
Take the opposite :
10000000 00000000 00000000 00001010
That is to say -10
⑤
int main()
{
char a[1000];
int i;
for(i=0; i<1000; i++)
{
a[i] = -1-i;
}
printf("%d",strlen(a));
return 0;
}
char The range of integers that the type can store is -128~127
The above example is deposited in sequence -1 -2 …… -128 , When you want to store -129 when
-129 Of
Original code :10000000 00000000 00000000 10000001
Inverse code :11111111 11111111 11111111 01111110
Complement code :11111111 11111111 11111111 01111111
Store low 8 individual bit position 01111111, So I want to store -129 When actually deposited 127, After that, it is deposited into 126~0
So the deposit is -1 , -2 …… -128, 127 …… 1 , 0
The length is 128+127=255
⑥
#include <stdio.h>
unsigned char i = 0;
int main()
{
for(i = 0;i<=255;i++)
{
printf("hello world\n");
}
return 0;
}
The result of this example is dead circulation , Because there is no sign char When storing integers The scope is 0~255
When stored 255 When is 11111111
Add one more and change it into 00000000
Fall into a dead cycle
⑦
#include <stdio.h>
int main()
{
unsigned int i;
for (i = 9; i >= 0; i--)
{
printf("%u\n", i);
}
}
The running result of this example is also dead cycle , Because unsigned numbers are always greater than or equal to 0!
边栏推荐
- Differences among opencv versions
- What is devsecops? Definitions, processes, frameworks and best practices for 2022
- BGP advanced experiment
- RHCE day 3
- From programmers to large-scale distributed architects, where are you (I)
- Container cloud notes
- 【Day1】 deep-learning-basics
- Safety reinforcement learning based on linear function approximation safe RL with linear function approximation translation 1
- leetcode842. Split the array into Fibonacci sequences
- Batch distribution of SSH keys and batch execution of ansible
猜你喜欢
Hands on deep learning (46) -- attention mechanism
Fabric of kubernetes CNI plug-in
【Day1】 deep-learning-basics
PHP代码审计3—系统重装漏洞
uniapp 小于1000 按原数字显示 超过1000 数字换算成10w+ 1.3k+ 显示
BGP ---- border gateway routing protocol ----- basic experiment
Hands on deep learning (39) -- gating cycle unit Gru
Evolution from monomer architecture to microservice architecture
用数据告诉你高考最难的省份是哪里!
Basic data types of MySQL
随机推荐
Dynamic address book
Hands on deep learning (45) -- bundle search
Devop basic command
Rhcsa day 9
OSPF comprehensive experiment
El Table Radio select and hide the select all box
System. Currenttimemillis() and system Nanotime (), which is faster? Don't use it wrong!
Batch distribution of SSH keys and batch execution of ansible
Dos:disk operating system, including core startup program and command program
按键精灵打怪学习-识别所在地图、跑图、进入帮派识别NPC
Native div has editing ability
uniapp---初步使用websocket(长链接实现)
MPLS: multi protocol label switching
Rhcsa learning practice
Si vous ne connaissez pas ces quatre modes de mise en cache, vous osez dire que vous connaissez la mise en cache?
Hands on deep learning (37) -- cyclic neural network
Some summaries of the third anniversary of joining Ping An in China
用数据告诉你高考最难的省份是哪里!
Differences among opencv versions
Exercise 7-3 store the numbers in the array in reverse order (20 points)