将 ~0u 赋值给 C++ 中的变量意味着什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26555159/
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-08-28 11:37:43  来源:igfitidea点击:

What does the assignment of ~0u to a variable mean in C++?

c++

提问by ryeager

I understand 0umeans 0 unsigned, but what does the ~at the beginning mean? Does it signify inversion in this case or does it mean something else?

我明白0u意味着 0 无符号,但~开头的意思是什么?在这种情况下,它表示倒置还是表示其他含义?

回答by cdhowie

It indicates bitwise not; all bits in the integer will be flipped, which in this case produces a number where all bits are 1.

它表示按位不;整数中的所有位都将被翻转,在这种情况下会产生一个所有位都为 1 的数字。

Note that since it is unsigned, if the integer is widened during assignment the extended bits would be 0. For example assuming that unsigned shortis 2 bytes and unsigned intis 4:

请注意,由于它是无符号的,如果整数在赋值期间被加宽,则扩展位将为 0。例如,假设它unsigned short是 2 个字节并且unsigned int是 4:

unsigned short s = ~0u; // 0xFFFF
unsigned int i = s;     // 0x0000FFFF

If you need to invert the bits of some generic numeric type Tthen you can use the construct ~(T(0)).

如果您需要反转某些通用数字类型的位,T则可以使用构造~(T(0)).

回答by Vlad from Moscow

This operator is better described in the C Standard than in the C++ Standard

这个运算符在 C 标准中比在 C++ 标准中描述得更好

4 The result of the ~ operator is the bitwise complement of its (promoted) operand (that is, each bit in the result is set if and only if the corresponding bit in the converted operand is not set). The integer promotions are performed on the operand, and the result has the promoted type. If the promoted type is an unsigned type, the expression ~E is equivalent to the maximum value representable in that type minus E.

4 ~ 运算符的结果是其(提升的)操作数的按位补码(即,当且仅当转换后的操作数中的相应位未设置时,结果中的每一位都被设置)。对操作数执行整数提升,结果具有提升的类型。如果提升的类型是无符号类型,则表达式 ~E 等效于该类型中可表示的最大值减去 E。

Thus ~0umeans the maximum value of an object of type unsigned int when each bit of its internal representation is set to 1.

因此~0u意味着当其内部表示的每一位设置为 1 时,类型为 unsigned int 的对象的最大值。

Consider using the operator that to set for example the first n bits to 1. The expression will look like

例如,考虑使用运算符 that 将前 n 位设置为 1。表达式将如下所示

~( ~0u << n )

If you want to set n bits starting from m position then you can write

如果你想从 m 位置开始设置 n 位,那么你可以写

~( ~0u << n ) << m

回答by Marco A.

It means a bitwise notwhich flips all the bits of the integer value by giving you a number with all bits set to 1.

这意味着按位不翻转整数值的所有位,方法是为您提供一个所有位都设置为 1 的数字。

(Assuming a 32 bit uint)
   0u
   00000000 00000000 00000000 00000000

   ~0u
   11111111 11111111 11111111 11111111

   3
   00000000 00000000 00000000 00000011

   ~3
   11111111 11111111 11111111 11111100

Ifthe machine uses a 2's complementrepresentation for negative integers, then casting ~0uto a signed integer is equivalent to -1. More on this in Stack Overflow question Is there a difference between -1 and ~0?.

如果机器对负整数使用2 的补码表示,则转换~0u为有符号整数等效于-1。Stack Overflow 问题中的更多相关信息-1 和 ~0 之间有区别吗?.

回答by user3965261

It is to invert the values in bits. For example:

它是以位为单位反转值。例如:

00000000000000000001

00000000000000000001

to

11111111111111111110

11111111111111111110

This is what ~does.

这就是~它的作用。