[英]What does ~0 mean in this code?
What's the meaning of ~0 in this code? 这段代码中〜0的含义是什么?
Can somebody analyze this code for me? 有人可以为我分析这段代码吗?
unsigned int Order(unsigned int maxPeriod = ~0) const
{
Point r = *this;
unsigned int n = 0;
while( r.x_ != 0 && r.y_ != 0 )
{
++n;
r += *this;
if ( n > maxPeriod ) break;
}
return n;
}
~0
is the bitwise complement of 0
, which is a number with all bits filled. ~0
是按位求补0
,这与填充所有位的数字。 For an unsigned 32-bit int, that's 0xffffffff
. 对于无符号的32位int,即
0xffffffff
。 The exact number of f
s will depend on the size of the value that you assign ~0
to. f
s的确切数量取决于您指定的值的大小~0
。
It's the one complement, which inverts all bits. 它是一个补码,它反转所有位。
~ 0101 => 1010
~ 0000 => 1111
~ 1111 => 0000
As others have mentioned, the ~
operator performs bitwise complement. 正如其他人所提到的,
~
运算符执行按位补码。 However, the result of performing the operation on a signed value is not defined by the standard. 但是,对标志值执行操作的结果不是由标准定义的。
In particular, the value of ~0
need not be -1
, which is probably the value intended. 特别是,
~0
的值不必是-1
,这可能是预期的值。 Setting the default argument to 将默认参数设置为
unsigned int maxPeriod = -1
would make maxPeriod
contain the highest possible value (signed to unsigned conversion is defined as an assignment modulo 2**n
, where n
is a characteristic number of the given unsigned type (the number of bits of representation)). 将使
maxPeriod
包含最高可能值(签名到无符号转换被定义为赋值模2**n
,其中n
是给定无符号类型的特征数(表示的位数))。
Also note that default arguments are not valid in C. 另请注意,默认参数在C中无效。
It's a binary complement function. 它是二进制补码函数。
Basically it means flip each bit. 基本上它意味着翻转每一位。
它是0的按位补码,在这个例子中,它是一个int,所有位都设置为1.如果sizeof(int)是4,那么数字是0xffffffff。
Basically, it's saying that maxPeriod has a default value of UINT_MAX. 基本上,它说maxPeriod的默认值为UINT_MAX。 Rather than writing it as UINT_MAX, the author used his knowledge of complements to calculate the value.
作者不是将其写成UINT_MAX,而是利用他对补语的知识来计算价值。
If you want to make the code a bit more readable in the future, include 如果您希望将来的代码更具可读性,请加入
#include <limits>
and change the call to read 并将调用更改为read
unsigned int Order(unsigned int maxPeriod = UINT_MAX) const
Now to explain why ~0 is UINT_MAX. 现在解释为什么~0是UINT_MAX。 Since we are dealing with an int, in which 0 is represented with all zero bits (00000000).
因为我们正在处理一个int,其中0表示所有零位(00000000)。 Adding one would give (00000001), adding one more would give (00000010), and one more would give (00000011).
添加一个会给(00000001),再添加一个会给(00000010),还有一个会给(00000011)。 Finally one more addition would give (00000100) because the 1's carry.
最后再添加一个(00000100)因为1的携带。
For unsigned ints, if you repeat the process ad-infiniteum, eventually you have all one bits (11111111), and adding another one will overflow the buffer setting all the bits back to zero. 对于无符号整数,如果重复进程ad-infiniteum,最终你有一个位(11111111),并且添加另一个将溢出缓冲区设置所有位回零。 This means that all one bits in an unsigned number is the maximum that data type (int in your case) can hold.
这意味着无符号数中的所有位都是数据类型(在您的情况下为int)可以容纳的最大值。
The "~" operation flips all bits from 0 to 1 or 1 to 0, flipping a zero integer (which has all zero bits) effectively gives you UINT_MAX. “〜”操作将所有位从0翻转为1或从1翻转为0,翻转零整数(具有全零位)有效地为您提供UINT_MAX。 So he basically the previous coded opted to computer UINT_MAX instead of using the system defined copy located in
#include <limits.h>
所以他基本上以前编码选择了计算机UINT_MAX而不是使用位于
#include <limits.h>
的系统定义副本
In the example it is probably an attempt to generate the UINT_MAX value. 在该示例中,可能尝试生成UINT_MAX值。 The technique is possibly flawed for reasons already stated.
由于已经陈述的原因,该技术可能存在缺陷。
The expression does however does have legitimate use to generate a bit mask with all bits set using a literal constant that is type-width independent; 然而,该表达式确实有合法的用途来生成位掩码,其中所有位都使用与字段宽度无关的文字常量设置; but that is not how it is being used in your example.
但这不是你的例子中使用它的方式。
As others have said, ~ is the bitwise complement operator (sometimes also referred to as bitwise not). 正如其他人所说,〜是按位补码运算符(有时也称为按位运算符)。 It's a unary operator which means that it takes a single input.
它是一元运算符,这意味着它只需要一个输入。
Bitwise operators treat the input as a bit pattern and perform their respective operations on each individual bit then return the resulting pattern. 按位运算符将输入视为位模式,并对每个位执行各自的操作,然后返回结果模式。 Applying the ~ operator to the bit pattern will negate each bit (each zero becomes a one, each one becomes a zero).
将〜运算符应用于位模式将取消每个位(每个零变为1,每个变为零)。
In the example you gave, the bit representation of the integer 0 is all zeros. 在您给出的示例中,整数0的位表示全为零。 Thus, ~0 will produce a bit pattern of all ones.
因此,~0将产生所有1的位模式。 Even though 0 is an int, it is the bit pattern ~0 that is assigned to maxPeriod (not the int value that would be represented by said bit pattern).
即使0是int,也是分配给maxPeriod的位模式~0(不是由所述位模式表示的int值)。 Since maxPeriod is an unsigned int, it is assigned the unsigned int value represented by ~0 (a pattern of all ones), which is in fact the highest value that an unsigned int can store before wrapping around back to 0.
由于maxPeriod是一个unsigned int,因此它被赋予了由~0(所有1的模式)表示的unsigned int值,这实际上是unsigned int在回绕到0之前可以存储的最高值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.