当前位置:网站首页>整型提升和大小端字节序
整型提升和大小端字节序
2022-06-28 06:12:00 【花海ovo】
以一个整形类型的数字1为例 他的二进制表示为
000000000000000000000000000000001;
而在计算机储存当中 他的数据保存为十六进制,即
0x 00 00 00 01
从左到右为数据的高位到低位,而计算机中的内存也对应着高内存到低内存,那么便有了大小端字节序的概念
大端字节序:指数据的高位保存在低地址处,数据的低位保存在高地址处
小端字节序:指数据的高位保存在高地址处,数据的低位保存在低地址处
那么我们如何判断我们所使用的vs在存储数据的时候是使用大端字节序还是小端字节序呢,我们可以创建一个char类型的指针指向整型类型的数字1,因为char*只能从高到低访问一个字节,所以我们便可以通过char指针访问1的第一个字节,对他解应用看看他是0还是1,从而判断它存储的大小端
代码如下:
char*p=NULL;//初始化指针
int a=1;
p=&a;//p指向a中的一个字节
if(*p)
printf("是大端");
else
printf("是小端");
//看看p解应用后访问的a中具体字节
那什么是整型提升呢
我们来看一段代码
int main(){
char a=-1;
signed char b=-1;
unsigned char c=-1;
printf("%d %d %d",a,b,c);那么a,b,c的结果分别是多少呢;
我们知道,数字在内存中是以补码的形式储存起来的 -1的原码为
10000000 00000000 000000000 00000001
除了符号位也就是最高位以外按位取反 得到反码;
11111111 11111111 11111111 11111110;
反码加一得到补码
11111111 11111111 11111111 11111111;
然而char类型只能存储一个字节 所以会发生截断 取低位的一个字节 即
11111111
使用a进行计算或者以%d输出a的时候会进行整型提升看一个字节中最高位;若为1则前面加上三个比特位全为1jiayi的字节;最高位为0则前面补0
现在为11111111 最高位为1 故补完以后为
11111111 11111111 11111111 11111111为补码
再将它加一取反得到源码并输出 结果为-1;
同理 b和a一样也是会发生整型提升;char类型默认就是有符号的signed char;
而unsigned char 不会这样,因为定义时时将它设置为无符号字符型的 所以它认为被截取的最高位不是符号位而是数字位 所以整型提升时前面全补零
即用%d输出c时候会被提升为
00000000 00000000 00000000 11111111
最高位符号位为零 所以是正数 正数的原反补码都相同 所以直接输出为256
整型提升的意义是怎样的呢

边栏推荐
- Using pytorch and tensorflow to calculate the confusion matrix of classification model
- 链表(一)——移除链表元素
- bash install. SH ******** error
- AutoCAD C# 多段线小锐角检测
- Failed to start component [StandardEngine[Catalina].StandardHost[localhost]]
- Caused by: com.fasterxml.jackson.databind.exc.InvalidFormatException:异常解决
- Camx架构开UMD、KMD log以及dump图的方式
- Ethereum Classic的难度计算|猿创征文
- How to add live chat in your Shopify store?
- Use the SQL SELECT count distinct query statement to count the total number of unique values of a field in the database
猜你喜欢

Working principle of es9023 audio decoding chip

Linux MySQL implements root user login without password

移动广告发展动向:撬动存量,精细营销

Unity packaging webgl uses IIS to solve the error

5g network overall architecture

Sharing tips for efficient scripting

ROS rviz_ Satellite function package visualizes GNSS track and uses satellite map

Lenovo hybrid cloud Lenovo xcloud, new enterprise IT service portal

How to add live chat in your Shopify store?

Lombok @equalsandhashcode annotation how to make objects The equals () method compares only some attributes
随机推荐
No one can only use foreach to traverse arrays, right?
YYGH-BUG-03
lombok @EqualsAndHashCode 注解如何让对象.equals()方法只比较部分属性
Ape pink ape power - Developer activity attack!
Sharing tips for efficient scripting
ROS rviz_ Satellite function package visualizes GNSS track and uses satellite map
Yygh-8-appointment registration
Maskrcnn, fast RCNN, fast RCNN excellent video
The length of pytorch dataloader the difference between epoch and iteration
Valueerror: iterative over raw text documents expected, string object received
AutoCAD C # Polyline Small Sharp angle Detection
Simple handwritten debounce function
慢内容广告:品牌增长的长线主义
Main functions of 5ggnb and ng ENB
JDBC学习(一)——实现简单的CRUD操作
Academic search related papers
Sklearn Feature Engineering (summary)
[MySQL] all query tables contain 20million data -- how to optimize SQL
Using pytorch and tensorflow to calculate the confusion matrix of classification model
Example of MVVM framework based on kotlin+jetpack