C - 需要一次将无符号整型向右移动一个位置。
我需要将无符号整数向右移动超过 32 次,并且仍然得到正确的答案零,而不是随机数或原始数。例如8>> 40 应该 = 0 但它返回一个随机数。
我知道一次向右移动一个位置的循环可以解决这个问题,因为它会在运行过程中填充零。然而,由于某种原因,我当前的代码不起作用。我做错了什么?
unsigned int shiftR(unsigned int a, unsigned int b) {
unsigned int i=0;
while (i < b) {
a >> 1;
i++;
}
return a;
}
这给了我一个编译警告,它没有效果(a>>1;)。怎么会?
谢谢!
I need to shift an unsigned int to the right more than 32 times and still get a proper answer of zero instead of the random or original number. E.g 8 >> 40 should = 0 but it returns a random number.
I understand a loop that shifts one place right at a time would solve this problem as it would fill in zeros as it went. However my current code for this doesn't work for some reason. What am I doing wrong?
unsigned int shiftR(unsigned int a, unsigned int b) {
unsigned int i=0;
while (i < b) {
a >> 1;
i++;
}
return a;
}
This gives me a compile warning that it has no effect ( a >> 1;). How come?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您想使用
a >>= 1;
或a = a >>> 1;
这是因为a>> 1
将 a 右移一次并返回结果。它不会将结果分配给a
You want to use
a >>= 1;
ora = a >> 1;
this is becausea >> 1
shifts a to the right once and returns the result. It doesn't assign the result toa
……然后就那么做吗?
为什么要把事情复杂化呢?
... Then do that?
Why complicate things?
据我记得C,你需要说 a = a >>> 1.
As far as I remember C, you need to say a = a >> 1.
正如其他人指出的那样,您从未重新分配过新值;该语句的结果没有用于任何用途,因此编译器将其删除。
a>>=1
就是你想要的。我想补充一点,如果您希望
unsigned int
为 32 位,则强制它。使用 C99stdint.h
库并使其成为uint32_t
- 漂亮且明确。As others noted, you never re-assigned
a
a new value; the result of that statement was not used for anything, so the compiler strips it out.a>>=1
is what you wanted.I'd like to add though that if you want your
unsigned int
to be 32-bit, then force it. Use the C99stdint.h
library and make ituint32_t
- nice and unambiguous.您必须更改
a >>> 1;
到a>>= 1;
。在 C 中,将整数左移或右移多于整数类型 [0] 的位宽度是未定义行为。踩到未定义的行为是非常糟糕的,因为什么都不会发生,不好的事情可能立即发生,不好的事情可能在未来的未知时刻发生,或者不好的事情可能发生在不同的平台或编译器上。
处理这个问题的正确方法是手动检查移动的位置是否超过 32 位,然后手动给出结果 0。
[0]: http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html 。您应该阅读整个页面,但具体部分是“超大班次金额”。
You must change
a >> 1;
toa >>= 1;
.In C, it is undefined behavior to left or right shift an integer by more places than the bit width of the integer type[0]. Stepping on undefined behavior is very bad, because nothing could happen, something bad could happen immediately, something bad could happen at an unknown point in the future, or something bad could happen on a different platform or compiler.
The correct way to deal with this is to manually check if you're shifting by more than 32 places, and then manually give a result of 0.
[0]: http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html . You should read the whole page, but the specific section is "Oversized Shift Amounts".
a + 1
将1
与a
相加,但不会在任何地方存储任何内容,因此a
的值未修改。要使用增量值更新a
的值,您必须执行a = a + 1
或a += 1
。类似于将a
中的整数值移位1
,然后将移位后的值存储在a
中,您需要执行a = a > ;> 1
或a>>= 1
。因为只做
a >>> 1
不会修改a
的值,编译器会适当地警告您该语句无效,这意味着保留该语句或删除它并不重要,因为它不会修改任何事物。在您的情况下,您要多次移动
a
、b
的值,因此您可以简单地使用a >>= b
而不是迭代一个循环。a + 1
adds1
witha
but does not store anything anywhere, therefore the value ofa
is unmodified. To update the value ofa
with the incremented value you have to doa = a + 1
ora += 1
. similarly to shift the integer value ina
by1
and then store the shifted value ina
you need to doa = a >> 1
ora >>= 1
.Because only doing
a >> 1
does not modify the value ofa
, the compiler appropriately warns you that this statement has no effect, that means that keeping this statement or removing it does not matter, as it does not modify anything.In your case you are shifting the value of
a
,b
nos of times so you can simply usea >>= b
instead of iterating a loop.