有符号右移=奇怪的结果?
我正在帮助某人做作业,遇到了这个奇怪的问题。问题是编写一个函数来反转有符号整数的字节顺序(无论如何,这就是函数的指定方式),这就是我想出的解决方案:
int reverse(int x)
{
int reversed = 0;
reversed = (x & (0xFF << 24)) >> 24;
reversed |= (x & (0xFF << 16)) >> 8;
reversed |= (x & (0xFF << 8)) << 8;
reversed |= (x & 0xFF) << 24;
return reversed;
}
如果将 0xFF000000
传递给此函数中,第一次赋值将导致0xFFFFFFFF
。我不太明白发生了什么,但我知道这与有符号和无符号之间的来回转换或类似的事情有关。
如果我将 ul
附加到 0xFF
,它就可以正常工作,我认为这是因为它被迫无符号,然后转换为有符号或朝那个方向的东西。生成的代码也会发生变化;如果没有 ul
说明符,它会使用 sar(算术右移),但作为无符号,它会按预期使用 shr。
如果有人能为我阐明这一点,我将非常感激。我应该知道这些东西,而且我以为我知道,但我真的不确定这里发生了什么。
提前致谢!
I was helping someone with their homework and ran into this strange issue. The problem is to write a function that reverses the order of bytes of a signed integer(That's how the function was specified anyway), and this is the solution I came up with:
int reverse(int x)
{
int reversed = 0;
reversed = (x & (0xFF << 24)) >> 24;
reversed |= (x & (0xFF << 16)) >> 8;
reversed |= (x & (0xFF << 8)) << 8;
reversed |= (x & 0xFF) << 24;
return reversed;
}
If you pass 0xFF000000
to this function, the first assignment will result in 0xFFFFFFFF
. I don't really understand what is going on, but I know it has something to do with conversions back and forth between signed and unsigned, or something like that.
If I either append ul
to 0xFF
it works fine, which I assume is because it's forced to unsigned then converted to signed or something in that direction. The resulting code also changes; without the ul
specifier it uses sar(shift arithmetic right), but as unsigned it uses shr as intended.
I would really appreciate it if someone could shed some light on this for me. I'm supposed to know this stuff, and I thought I did, but I'm really not sure what's going on here.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
由于
x
是一个有符号数量,因此(x & (0xFF << 24))
的结果是0xFF000000,它也是有符号的由于最高(符号)位被设置,因此是一个负数。int
(有符号值)上的>>
运算符执行符号扩展(编辑:尽管此行为未定义且特定于实现)并在该值右移时传播符号位值 1。您应该按如下方式重写该函数以专门处理无符号值:
Since
x
is a signed quantity, the result of(x & (0xFF << 24))
is 0xFF000000 which is also signed and thus a negative number since the top (sign) bit is set. The>>
operator onint
(a signed value) performs sign extension (Edit: though this behaviour is undefined and implementation-specific) and propagates the sign bit value of 1 as the value is shifted to the right.You should rewrite the function as follows to work exclusively on unsigned values:
从您的结果我们可以推断您使用的是 32 位计算机。
在此表达式中,
0xFF
是一个int
,因此0xFF << 24
也是一个int
,x
也是如此。当您在两个
int
之间执行按位&
时,结果也是一个int
,在本例中,值为0xFF000000< /code> 在 32 位机器上意味着符号位被设置,所以你有一个负数。
对具有负值的有符号类型的对象执行右移的结果是实现定义的。在您的情况下,执行保留符号的算术右移。
如果右移无符号类型,那么您将获得字节反转函数所期望的结果。您可以通过将按位
&
操作数的任一操作数设置为无符号类型来强制将两个操作数转换为无符号类型来实现此目的。 (对于任何有符号 int 无法容纳无符号 int 的所有可能的正值范围的实现都是如此,这几乎是所有实现。)From your results we can deduce that you are on a 32-bit machine.
In this expression
0xFF
is anint
, so0xFF << 24
is also anint
, as isx
.When you perform the bitwise
&
between twoint
, the result is also anint
and in this case the value is0xFF000000
which on a 32-bit machine means that the sign bit is set, so you have a negative number.The result of performing a right-shift on an object of signed type with a negative value is implementation-defined. In your case, as sign-preserving arithmetic shift right is performed.
If you right-shift an unsigned type, then you would get the results that you were expecting for a byte reversal function. You could achieve this by making either operand of the bitwise
&
operand an unsigned type forcing conversion of both operands to the unsigned type. (This is true on any implementation where an signedint
can't hold all the possible range of positive values of anunsigned int
which is nearly all implementations.)有符号类型的右移是实现定义的,特别是编译器可以随意进行算术或逻辑移位。如果您正在处理的具体值为正值,您将不会注意到这一点,但一旦它为负值,您可能会陷入陷阱。
只是不要这样做,这不是便携式的。
Right shift on signed types is implementation defined, in particular the compiler is free to do an arithmetic or logical shift as pleases. This is something you will not notice if the concrete value that you are treating is positive, but as soon as it is negative you may fall into a trap.
Just don't do it, this is not portable.
x
有符号,因此最高位用于符号。 0xFF000000 表示“负 0x7F000000”。当您进行移位时,结果是“符号扩展”:添加在左侧以替换右移的前一个 MSB 的二进制数字始终与值的符号相同。因此,如果要移位的值是无符号的,或者向左移位,则新位将为 0。只有在有符号值的右移中,符号扩展才会发挥作用。
x
is signed, so the highest bit is used for the sign. 0xFF000000 means "negative 0x7F000000". When you do the shift, the result is "sign extended": The binary digit that is added on the left to replace the former MSB that was shifted right, is always the same as the sign of value. SoIf the value being shifted is unsigned, or if the shift is toward the left, the new bit would be 0. It's only in right-shifts of signed values that sign-extension come into play.
如果您希望它在具有有符号和无符号整数的所有平台上同样工作,请更改
为
If you want it to work the same on al platforms with both signed and unsigned integers, change
into
如果这是 java 代码,您应该使用 '>>>'这是一个无符号右移,否则它将对值进行符号扩展
If this is java code you should use '>>>' which is an unsigned right shift, otherwise it will sign extend the value