C语言 C中的1U和1有什么区别吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4192440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:05:02  来源:igfitidea点击:

Is there any difference between 1U and 1 in C?

cinteger

提问by arr

    while ((1U << i) < nSize) {
        i++;
    }

Any particular reason to use 1Uinstead of 1?

使用1U代替的任何特殊原因1

回答by Dietrich Epp

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 theory1U << iis more portable than 1 << i. In practice all C compilers you'll ever encounter treat signed left shifts the same as unsigned left shifts.

在大多数编译器上,两者都会给出具有相同表示的结果。但是,根据 C 规范,对有符号参数进行位移操作的结果给出了实现定义的结果,因此理论上1U << i比 更可移植1 << i。在实践中,您将遇到的所有 C 编译器都将有符号左移视为与无符号左移相同。

The other reason is that if nSizeis unsigned, then comparing it against a signed 1 << iwill generate a compiler warning. Changing the 1to 1Ugets rid of the warning message, and you don't have to worry about what happens if iis 31 or 63.

另一个原因是,如果nSize是无符号的,那么将其与有符号的进行比较1 << i将生成编译器警告。更改11U可以消除警告消息,并且您不必担心如果i是 31 或 63会发生什么。

The compiler warning is most likely the reason why 1Uappears in the code. I suggest compiling C with most warnings turned on, and eliminating the warning messages by changing your code.

编译器警告很可能是1U出现在代码中的原因。我建议在打开大多数警告的情况下编译 C,并通过更改代码来消除警告消息。

回答by AlexanderMP

1U is unsigned. It can carry values twice as big, but without negative values.

1U 是无符号的。它可以携带两倍大的值,但没有负值。

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.

根据环境的不同,在使用 U 时,i 的最大值可以为 31 或 15,而不会导致溢出。如果不使用 U,i 最多可以是 30 或 14。

31, 30 are for 32 bit int
15, 14 are for 16 bit int

31, 30 代表 32 位整数
15, 14 代表 16 位整数

回答by steabert

If nSize is an int, it can be maximum of 2147483647 (2^31-1). If you use 1instead of 1Uthen 1 << 30will get you 1073741824 and 1 << 31will be -2147483648, and so the while loop will never end if nSize is larger than 1073741824.

如果 nSize 是int,则最大值为 2147483647 (2^31-1)。如果使用1而不是1Uthen1 << 30将得到 1073741824 并且1 << 31将是 -2147483648,因此如果 nSize 大于 1073741824,while 循环将永远不会结束。

With 1U << i, 1U << 31will 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 than 1U << 31.

使用1U << i,1U << 31将评估为 2147483648,因此您可以安全地将它用于 nSize 到 2147483647。如果 nSize 是无符号整数,则循环也可能永远不会结束,因为在这种情况下 nSize 可能大于1U << 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...

编辑:所以我不同意告诉你 nSize 应该是无符号的答案,但如果它是有符号的,那么它不应该是负数......

回答by peoro

1U is unsigned.

1U 是无符号的。

The reason why they used an unsigned value in that is expression is (I guess) because nSizeis unsigned too, and compilers (when invoked with certain parameters) give warnings when comparing a signed and an unsigned values.

他们在表达式中使用无符号值的原因是(我猜)因为nSize也是无符号的,并且编译器(当使用某些参数调用时)在比较有符号和无符号值时会发出警告。

Another reason (less likely, in my opinion, but we cannot know without knowing wath value nSizeis supposed to assume) is that unsigned values can be twice as big as signed ones, so nSizecould be up to ~4*10^9 instead of ~2*10^9.

另一个原因(在我看来不太可能,但我们在不知道nSize应该假设的值的情况下无法知道)是无符号值可以是有符号值的两倍,因此nSize可能高达 ~4*10^9 而不是 ~ 2*10^9。