C语言中的1U和1有什么区别吗?
while ((1U << i) < nSize) {
i++;
}
使用 1U
而不是 1
有什么特殊原因吗?
while ((1U << i) < nSize) {
i++;
}
Any particular reason to use 1U
instead of 1
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在大多数编译器上,两者都会给出具有相同表示的结果。然而,根据 C 规范,对有符号参数进行位移操作的结果给出了实现定义的结果,因此在理论上
1U << i
比1 << 更便携。我。实际上,您遇到的所有 C 编译器都将有符号左移视为与无符号左移相同。
另一个原因是,如果
nSize
是无符号的,则将其与有符号的1 << 进行比较。 i
将生成编译器警告。将1
更改为1U
可以消除警告消息,并且您不必担心i
为 31 或 63 时会发生什么 。编译器警告很可能是代码中出现
1U
的原因 我建议在打开大多数警告的情况下编译 C,并通过更改代码来消除警告消息。On most compliers, both will give a result with the same representation. However, according to the C specification, the result of a bit shift operation on a signed argument gives implementation-defined results, so in theory
1U << i
is more portable than1 << i
. In practice all C compilers you'll ever encounter treat signed left shifts the same as unsigned left shifts.The other reason is that if
nSize
is unsigned, then comparing it against a signed1 << i
will generate a compiler warning. Changing the1
to1U
gets rid of the warning message, and you don't have to worry about what happens ifi
is 31 or 63.The compiler warning is most likely the reason why
1U
appears in the code. I suggest compiling C with most warnings turned on, and eliminating the warning messages by changing your code.1U 未签名。它可以携带两倍大的值,但不能携带负值。
根据环境的不同,使用 U 时,i 的最大值可以为 31 或 15,而不会导致溢出。不使用 U 时,i 最大可为 30 或 14。31、30
表示 32 位 int
15、14 表示 16 位 int
1U is unsigned. It can carry values twice as big, but without negative values.
Depending on the environment, when using U, i can be a maximum of either 31 or 15, without causing an overflow. Without using U, i can be a maximum of 30 or 14.
31, 30 are for 32 bit int
15, 14 are for 16 bit int
如果 nSize 是
int
,则其最大值可为 2147483647 (2^31-1)。如果您使用1
而不是1U
则1 << 30
将为您提供 1073741824 和1 << 31
将是 -2147483648,因此如果 nSize 大于 1073741824,则 while 循环将永远不会结束。我,
1U << 31
将计算为 2147483648,因此您可以安全地将其用于 nSize 最大为 2147483647。如果 nSize 是无符号整数,则循环也可能永远不会结束,因为在这种情况下 nSize 可以大于 <代码>1U << 31。编辑:所以我不同意告诉你 nSize 应该是无符号的答案,但如果它是有符号的,那么它不应该是负数......
If nSize is an
int
, it can be maximum of 2147483647 (2^31-1). If you use1
instead of1U
then1 << 30
will get you 1073741824 and1 << 31
will be -2147483648, and so the while loop will never end if nSize is larger than 1073741824.With
1U << i
,1U << 31
will evaluate to 2147483648, and so you can safely use it for nSize up to 2147483647. If nSize is an unsigned int, it is also possible that the loop never ends, as in that case nSize can be larger than1U << 31
.Edit: So I disagree with the answers telling you nSize should be unsigned, but if it is signed then it should not be negative...
1U 未签名。
他们在该表达式中使用无符号值的原因是(我猜)因为 nSize 也是无符号的,并且编译器(当使用某些参数调用时)在比较有符号值和无符号值时会发出警告。
另一个原因(我认为不太可能,但如果不知道 nSize 应该假设的值,我们就无法知道)是无符号值可以是有符号值的两倍,因此 nSize< /code> 最多可达 ~4*10^9 而不是 ~2*10^9。
1U is unsigned.
The reason why they used an unsigned value in that is expression is (I guess) because
nSize
is unsigned too, and compilers (when invoked with certain parameters) give warnings when comparing a signed and an unsigned values.Another reason (less likely, in my opinion, but we cannot know without knowing wath value
nSize
is supposed to assume) is that unsigned values can be twice as big as signed ones, sonSize
could be up to ~4*10^9 instead of ~2*10^9.