当前位置:网站首页>storage of data in memory
storage of data in memory
2022-08-10 15:21:00 【7 on 7.】
一、数据类型
数据类型有7种:
char 字符型
short 短整型
int 整型
long 长整型
long long 更长整型
float 单精度浮点数
double 双精度浮点数
二、原码,反码,补码
计算机中的整数有三种2进制表示方法,即原码、反码和补码.
三种表示方法均有符号位和数值位两部分,符号位都是用0表示’正”,用1表示"负”,而数值位正数的原、反、补码都相同.
负整数的三种表示方法各不相同.
原码:It is to directly translate the value into binary in the form of positive and negative numbers to get the original code.
反码:原码的符号位不变,The other bits are reversed bit by bit to get the inverse code.
补码:反码加1,得到补码.
计算例子:如图
计算a+b:
a是正数,原码等于补码:00000000 00000000 00000000 00000111
b是负数,原码:10000000 00000000 00000000 00001010
反码:111111111 11111111 11111111 11110101
补码:11111111 11111111 11111111 11110110
a+bThe complements of , are added separately:11111111 11111111 11111111 11111101
而打印的是%d即有符号整型,to convert it to the original code,Subtract one and negate it to the original code:
10000000 00000000 00000000 00000011 再化为10进制就是-3
看下运行结果:
三、大小端
There are two ways to store data in memory, one is big endian and the other is little endian.
在计算机系统中,以字节为单位的,每个地址单元对应着一个字节,一个字节为8bit.但在C语言中除了8 bit的char之外,还有16 bit的short型,32bit的long型(要看具体的编译器),另外,对于位数大于8位的处理器,例如16位或者32位的处理器,Because the odd memory width is greater than one byte,Then there is a problem of how to arrange multiple bytes.This leads to a big-endian storage model and a little-endian storage model.
How can we tell the endianness of the current machine:
#include<stdio.h>
int is_sys()
{
int a = 1;
return (*(char *)&a);
}
int main()
{
int ret = is_sys();
if (ret == 1)
{
printf("小端\n");
}
else
{
printf("大端\n");
}
return 0;
}
Take an integer1,补码:0000000 0000000 00000000 00000001,To determine which storage mode it is,Just take out the first byte,Because low bits are big endian when placed at high addresses,Little endian when low bits are placed at low addresses,The address is first converted into a character pointer to ensure that a byte is taken ,Then dereference the content,如果是1就是小端,反之是大端.
整型提升
C的整型算术运算总是至少以缺省整型类型的精度来进行的.为了获得这个精度,表达式中的字符和短整型操作数在使用之前被转换为普通整型,这种转换称为整型.
Truncation occurs when we store data with character types,Because a number is four bytes,A character type can only store one byte,Look at the current sign bit after truncation:
对于有符号类型,如果是1就把1The previous bit completion1进行整型提升,如果是0就把0The previous bit completion0进行整型提升.
对于无符号类型,直接补全0.
(1)We can understand it well by looking at the following example:
#include<stdio.h>
int main()
{
char a = -2;
unsigned char b = -10;
printf("%d %d", a,b);
}
a=-2,原码:10000000 0000000 00000000 00000010
反码: 11111111 11111111 11111111 11111101
补码: 11111111 11111111 11111111 11111110
但是aIt is a character type that can only store one byte,A truncation will occur and only take the low-order one byte ie:11111110
And we print a signed integer%d,会发生整型提升,因为它是负的,所以在前面补1
11111111 11111111 11111111 11111110,The original code is printed,So convert to the original code.减一取反:10000000 00000000 00000000 00000010 结果是-2
b=-10,原码:10000000 00000000 00000000 00001010
反码: 11111111 11111111 11111111 11110101
补码 :11111111 11111111 11111111 11110110
Ditto after truncation:11110110 Because he is an unsigned integer in front0:
00000000 00000000 00000000 11110110.The direct print result of the original code is246.
Verify the result again:
(2)另外%u是打印无符号整型.It is also truncated to see if the original number has a sign,If there is a sign, it is not complemented1或补0,unsigned direct complement0.Then print it directly as the original code after the completion
例如 char=-128
原码:10000000 00000000 00000000 10000000
反码: 11111111 11111111 11111111 01111111
补码: 11111111 11111111 11111111 10000000
Because it is a character type, take the lower byte 1000000,And because he is a negative complement1:
11111111 11111111 11111111 10000000,Print directly as the original code If the factor is too large, see the result directly:
(3)One more analysis:
#include<stdio.h>
int main()
{
int i = -20;
unsigned char j = 10;
unsigned char b = i + j;
printf("%u", b);
}
i的原码:10000000 00000000 00000000 00010100;
反码: 11111111 1111111 11111111 11101011
补码: 11111111 11111111 11111111 11101100
而jis an integer-less character value 因为它是正数,原码等于补码:
00000000 00000000 00000000 00001010
两者相加是:11111111 11111111 11111111 11110110
And there is an unsigned char type,发生截断:11110110
而打印的是%uDirect integer boost complement0 补码等于原码 :
00000000 00000000 00000000 11110110结果是246
看结果:
边栏推荐
- 程序员=加班??——掌握时间才能掌握人生
- Based on Azuki Series: NFT Valuation Analysis Framework "DRIC"
- CSP-J1 CSP-S1 初赛 第1轮(2022.08.09)
- 学习MySQL 临时表
- 从洞察到决策,一文解读标签画像体系建设方法论
- 2022-08-10日报: Swin Transformer作者曹越加入智源,开展视觉基础模型研究
- SYM32——RTC实时时钟程序讲解
- Pagoda panel open Redis to specify the network machine
- mysql进阶(三十三)MySQL数据表添加字段
- Analysys and the Alliance of Small and Medium Banks jointly released the Hainan Digital Economy Index, so stay tuned!
猜你喜欢
随机推荐
易基因|深度综述:m6A RNA甲基化在大脑发育和疾病中的表观转录调控作用
无线网络、HTTP缓存、IPv6
丁香园
Mysql语句分析、存储引擎、索引优化等详情
Go Context基本使用
E. Cross Swapping(并查集变形/好题)
Appium进行APP自动化测试
富爸爸穷爸爸之读书笔记
PAT甲级 1014 排队等候(队列大模拟+格式化时间)
12海里、24海里、200海里的意义及名称
QOS function introduction
LeetCode_2598_剑指Offer Ⅱ 091.粉刷房子
Appium for APP automation testing
Websocket realizes real-time change of chart content
容器化 | 在 S3 实现定时备份
Flask框架——MongoEngine使用MongoDB数据库
2022年网络安全培训火了,缺口达95%,揭开网络安全岗位神秘面纱
scala 10种函数高级应用
scala集合
王学岗—————————哔哩哔哩直播-手写哔哩哔哩硬编码录屏推流(硬编)(26节课)









