什么是CHAR I = 0x80的,为什么溢出并没有发生位移 [英] what is char i=0x80 and why overflow did not happen in bit shifting

查看:351
本文介绍了什么是CHAR I = 0x80的,为什么溢出并没有发生位移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个程序

#include <stdio.h>
main()
{ unsigned char i=0x80;
printf("i=%d",i<<1);
}

它给输出为256。
我不是什么不明确

The output it is giving is 256. I am not clear with what does

unsigned char i=0x80; <-- i is not int it is char so what will it store?

我知道bitshift和十六进制的东西。
如何i的值存储,它是如何被更改为256?

I know bitshift and hexadecimal things. How is the value of i being stored and how does it gets changed to 256?

更新

为什么当移位操作没有发生溢出没有发生?

Why did overflow not occurred when the bit shift operation happened?

推荐答案

在C,一个字符是用于存储字符数据的整数类型,通常为1个字节。

In C, a char is an integer type used to store character data, typically 1 byte.

保存在 i的值 0x80的十六进制常数等于 128

The value stored in i is 0x80 a hexidecimal constant that is equal to 128.

在两个整数类型的算术运算(如 I&LT;&LT; 1 )将提升到的更宽的类型,在这种情况下,到 INT ,因为 1 是一个整型常量。在任何情况下,整函数参数都提升到int类型。

An arithmetic operation on two integer types (such as i << 1) will promote to the wider type, in this case to int, since 1 is an int constant. In any case, integer function arguments are promoted to int.

然后将结果发送到的printf ,以%d个格式说明,这意味着打印整数

Then you send the result to printf, with a %d format specifier, which mean "print an integer".

这篇关于什么是CHAR I = 0x80的,为什么溢出并没有发生位移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆