当前位置:网站首页>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!
边栏推荐
- El Table Radio select and hide the select all box
- MPLS: multi protocol label switching
- System. Currenttimemillis() and system Nanotime (), which is faster? Don't use it wrong!
- Native div has editing ability
- Leetcode48. Rotate image
- uniapp 处理过去时间对比现在时间的时间差 如刚刚、几分钟前,几小时前,几个月前
- PHP代码审计3—系统重装漏洞
- Rhsca day 11 operation
- AUTOSAR from getting started to mastering 100 lectures (106) - SOA in domain controllers
- MySQL develops small mall management system
猜你喜欢
随机推荐
Safety reinforcement learning based on linear function approximation safe RL with linear function approximation translation 2
Vanishing numbers
Two way process republication + routing policy
2021-08-10 character pointer
Number of relationship models
If the uniapp is less than 1000, it will be displayed according to the original number. If the number exceeds 1000, it will be converted into 10w+ 1.3k+ display
Es entry series - 6 document relevance and sorting
AUTOSAR from getting started to mastering 100 lectures (106) - SOA in domain controllers
Application of safety monitoring in zhizhilu Denggan reservoir area
Architecture introduction
Batch distribution of SSH keys and batch execution of ansible
Development guidance document of CMDB
Es advanced series - 1 JVM memory allocation
用数据告诉你高考最难的省份是哪里!
A little feeling
Whether a person is reliable or not, closed loop is very important
Uniapp--- initial use of websocket (long link implementation)
有老师知道 继承RichSourceFunction自定义读mysql怎么做增量吗?
Kotlin set operation summary
Tables in the thesis of latex learning