C - 需要一次将无符号整型向右移动一个位置。

发布于 2024-11-29 17:41:32 字数 386 浏览 1 评论 0原文

我需要将无符号整数向右移动超过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

懒的傷心 2024-12-06 17:41:32

您想使用 a >>= 1;a = a >>> 1;这是因为a>> 1 将 a 右移一次并返回结果。它不会将结果分配给 a

You want to use a >>= 1; or a = a >> 1; this is because a >> 1 shifts a to the right once and returns the result. It doesn't assign the result to a

祁梦 2024-12-06 17:41:32

我需要将无符号整数向右移动超过 32 次,但仍然得到正确的答案零,而不是随机数或原始数。

……然后就那么做吗?

unsigned int shiftR(unsigned int a, unsigned int b) {
  return (b >= 32) ? 0 : a >> b;
}

为什么要把事情复杂化呢?

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.

... Then do that?

unsigned int shiftR(unsigned int a, unsigned int b) {
  return (b >= 32) ? 0 : a >> b;
}

Why complicate things?

ι不睡觉的鱼゛ 2024-12-06 17:41:32

据我记得C,你需要说 a = a >>> 1.

As far as I remember C, you need to say a = a >> 1.

爱已欠费 2024-12-06 17:41:32

正如其他人指出的那样,您从未重新分配过新值;该语句的结果没有用于任何用途,因此编译器将其删除。 a>>=1 就是你想要的。

我想补充一点,如果您希望 unsigned int 为 32 位,则强制它。使用 C99 stdint.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 C99 stdint.h library and make it uint32_t - nice and unambiguous.

小帐篷 2024-12-06 17:41:32

一次向右移动一个位置的循环

您必须更改 a >>> 1;a>>= 1;

例如8>> 40 应该 = 0 但它返回一个随机数。

在 C 中,将整数左移或右移多于整数类型 [0] 的位宽度是未定义行为。踩到未定义的行为是非常糟糕的,因为什么都不会发生,不好的事情可能立即发生,不好的事情可能在未来的未知时刻发生,或者不好的事情可能发生在不同的平台或编译器上。

处理这个问题的正确方法是手动检查移动的位置是否超过 32 位,然后手动给出结果 0。

[0]: http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html 。您应该阅读整个页面,但具体部分是“超大班次金额”。

a loop that shifts one place right at a time

You must change a >> 1; to a >>= 1;.

E.g 8 >> 40 should = 0 but it returns a random number.

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".

全部不再 2024-12-06 17:41:32

a + 11a 相加,但不会在任何地方存储任何内容,因此 a 的值未修改。要使用增量值更新 a 的值,您必须执行 a = a + 1a += 1 。类似于将 a 中的整数值移位 1,然后将移位后的值存储在 a 中,您需要执行 a = a > ;> 1a>>= 1

因为只做 a >>> 1 不会修改 a 的值,编译器会适当地警告您该语句无效,这意味着保留该语句或删除它并不重要,因为它不会修改任何事物。

在您的情况下,您要多次移动 ab 的值,因此您可以简单地使用 a >>= b 而不是迭代一个循环。

a + 1 adds 1 with a but does not store anything anywhere, therefore the value of a is unmodified. To update the value of a with the incremented value you have to do a = a + 1 or a += 1 . similarly to shift the integer value in a by 1 and then store the shifted value in a you need to do a = a >> 1 or a >>= 1.

Because only doing a >> 1 does not modify the value of a, 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 use a >>= b instead of iterating a loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文