向左移位运算符 [英] Left shift operator
问题描述
如果我有以下几点:
char v = 32; // 0010 0000
然后我做的:
v << 2
数becames负。 // 1000 0000 -128
the number becames negative. // 1000 0000 -128
我读了标准,但它是只写了:
I read the standard but it is only written:
如果E1有符号类型和非负值,和E1×2 E2是
重新在结果类型$ P $ psentable,然后就是得到的值;
否则,行为是理解过程网络定义。
If E1 has a signed type and nonnegative value, and E1 × 2 E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined.
因此,如果是一个规则,我不明白,如果有点那张最左边位
数量必须开始为负。
so I don't understand if is a rule that if a bit goes on most left bit the number must begin negative.
我使用GCC。
推荐答案
左移两次将给予1000 0000) 2 = 128) 10 。
Left shifting it twice would give 1000 0000)2 = 128)10.
如果128是字符
,即你在某些机器(与支持的编译器)再presentable,提供了一个字符< /大小> 8位code>那么128将是你(因为它是重新presentable在这样一类)的值。
If 128 is representable in char
i.e. you're in some machine (with a supporting compiler) that provides a char
of size > 8 bits then 128 would be the value you get (since it's representable in such a type).
否则,如果大小的炭
就像最常见的机器,对于使用二的补负值的符号字符类型,[-128只是8比特, 127]是重新presentable范围。你未定义行为的土地,因为它不是重新presentable如,在这种类型的。
Otherwise, if the size of a char
is just 8 bits like most common machines, for a signed character type that uses two's complement for negative values, [-128, 127] is the representable range. You're in undefined behaviour land since it's not representable as-is in that type.
这篇关于向左移位运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!