这是一个程序
#include <stdio.h>
main()
{ unsigned char i=0x80;
printf("i=%d",i<<1);
}
Run Code Online (Sandbox Code Playgroud)
它给出的输出是256.我不清楚它是什么
unsigned char i=0x80; <-- i is not int it is char so what will it store?
Run Code Online (Sandbox Code Playgroud)
我知道bitshift和十六进制的东西.如何存储i的值以及如何将其更改为256?
UPDATE
为什么在位移操作发生时没有发生溢出?
sve*_*rre 18
在C中,a char是用于存储字符数据的整数类型,通常为1个字节.
存储的值i是0x80一个等于的十六进制常数128.
对两个整数类型(例如i << 1)的算术运算将推广到更宽的类型,在本例中为int,因为1是一个int常量.在任何情况下,整数函数参数都提升为int.
然后printf使用%d格式说明符将结果发送到"打印整数".