按位左移一个16的无符号字符是什么意思

我正在读取包含unsigned char变量的.cpp文件,它尝试按位左移16位,因为unsigned char由8位组成,左移16位将擦除所有位并用8位填充它。

unsigned char byte=0xff; byte << 16;

当你移动一个值时,

 unsigned char x = ...; int y = x << 16; 

如果unsigned char适合int (大多数系统),则x的类型被提升为int如果unsigned char不适合int (稀有1 ),则x的类型被提升为unsigned char 。 只要你的int是25位宽或更宽,那么就不会丢弃任何数据2

请注意,这与16具有int类型的事实完全无关。

 /* All three are exactly equivalent */ x << 16; x << 16u; x << (unsigned char) 16; 

资料来源:从n1516(C99草案):

§6.5.7第3段:按位移位运算符

对每个操作数执行整数提升。 结果的类型是提升的左操作数的类型。

§6.3.1.1第2段:布尔,字符和整数

如果int可以表示原始类型的所有值(由宽度限制,对于位字段),该值将转换为int; 否则,它将转换为unsigned int。 这些被称为整数促销。

脚注:

1 :已知某些DSP芯片以及某些Cray超级计算机具有sizeof(char) == sizeof(int) 。 这简化了处理器的加载存储单元的设计,但代价是额外的内存消耗。

2 :如果你的左移提升为int然后溢出int ,这是未定义的行为(恶魔可能会飞出你的鼻子)。 相比之下, unsigned溢出始终是明确定义的,因此通常应在unsigned类型上进行位移。

如果char适合int内部,它将被提升为int ,结果将如您所期望的那样。 如果不是这种情况,则根据标准是未定义的行为,并且可能会发出编译警告。 从标准:

The integer promotions are performed on each of the operands. The type of the result is that of the promoted left operand. If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.