当前位置:网站首页>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
看结果:
边栏推荐
- Analysys and the Alliance of Small and Medium Banks jointly released the Hainan Digital Economy Index, so stay tuned!
- 【语义分割】DeepLab系列
- Basic learning of XML
- 容器化 | 在 S3 实现定时备份
- Basic use of Go Context
- SWIG教程《二》
- Pagoda panel open Redis to specify the network machine
- 软件测试用例篇
- 数据在内存中的存储
- NFT digital collection development issue - digital collection platform
猜你喜欢

微信小程序,自定义输入框与导航胶囊对其

NFT数字藏品——数字藏品发行平台开发

Do not access Object.prototype method ‘hasOwnProperty‘ from target object....

Redis -- Nosql

奢侈品鉴定机构小程序开发制作功能介绍

Analysys and the Alliance of Small and Medium Banks jointly released the Hainan Digital Economy Index, so stay tuned!

Alibaba的秒杀系统—千亿级并发设计手册上线了

E. Cross Swapping (and check out deformation/good questions)

字节终面:CPU 是如何读写内存的?

E. Cross Swapping(并查集变形/好题)
随机推荐
学习MySQL 临时表
win2012安装Oraclerac失败
老板加薪!看我做的WPF Loading!!!
Introduction to the Internet (2)
解题-->在线OJ(十九)
E. Cross Swapping(并查集变形/好题)
兆骑科创创业赛事活动发布平台,创业赛事,项目路演
BFT机器人带你走进智慧生活 ——探索遨博机器人i系列的多种应用
常见SQL、API接口等常见约定
$‘\r‘: command not found
LeetCode_2598_剑指Offer Ⅱ 091.粉刷房子
数据类型与整型存储
Azure IoT Partner Technology Empowerment Workshop: IoT Dev Hack
fastposter v2.9.1 程序员必备海报生成器
小程序-语音播报功能
const修饰的指针变量(详解)
Digital Collection Platform System Development Practice
MySQL 原理与优化:Update 优化
易观分析联合中小银行联盟发布海南数字经济指数,敬请期待!
SWIG Tutorial "One"