在这段代码中,0表示什么?

时间:2022-10-11 19:58:51

What's the meaning of ~0 in this code?
Can somebody analyze this code for me?

这段代码中〜0的含义是什么?有人可以为我分析这段代码吗?

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;
}

8 个解决方案

#1


25  

~0 is the bitwise complement of 0, which is a number with all bits filled. For an unsigned 32-bit int, that's 0xffffffff. The exact number of fs will depend on the size of the value that you assign ~0 to.

〜0是0的按位补码,它是填充所有位的数字。对于无符号的32位int,即0xffffffff。 fs的确切数量取决于您指定的值的大小~0。

#2


17  

It's the one complement, which inverts all bits.

它是一个补码,它反转所有位。

 ~  0101 => 1010
 ~  0000 => 1111
 ~  1111 => 0000

#3


9  

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. Setting the default argument to

特别是,~0的值不必为-1,这可能是预期的值。将默认参数设置为

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中无效。

#4


4  

It's a binary complement function.

它是二进制补码函数。

Basically it means flip each bit.

基本上它意味着翻转每一位。

#5


4  

It is the bitwise complement of 0 which would be, in this example, an int with all the bits set to 1. If sizeof(int) is 4, then the number is 0xffffffff.

它是0的按位补码,在这个例子中,它是一个int,所有位都设置为1.如果sizeof(int)是4,那么数字是0xffffffff。

#6


3  

Basically, it's saying that maxPeriod has a default value of UINT_MAX. Rather than writing it as UINT_MAX, the author used his knowledge of complements to calculate the value.

基本上,它说maxPeriod的默认值为UINT_MAX。作者不是将其写成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. Since we are dealing with an int, in which 0 is represented with all zero bits (00000000). Adding one would give (00000001), adding one more would give (00000010), and one more would give (00000011). Finally one more addition would give (00000100) because the 1's carry.

现在解释为什么~0是UINT_MAX。因为我们正在处理一个int,其中0表示所有零位(00000000)。添加一个会给(00000001),再添加一个会给(00000010),还有一个会给(00000011)。最后再添加一个(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. This means that all one bits in an unsigned number is the maximum that data type (int in your case) can hold.

对于无符号整数,如果重复进程ad-infiniteum,最终你有一个位(11111111),并且添加另一个将溢出缓冲区设置所有位回零。这意味着无符号数中的所有位都是数据类型(在您的情况下为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. So he basically the previous coded opted to computer UINT_MAX instead of using the system defined copy located in #include <limits.h>

“〜”操作将所有位从0翻转为1或从1翻转为0,翻转零整数(具有全零位)有效地为您提供UINT_MAX。所以他基本上以前的编码选择了计算机UINT_MAX而不是使用#include 中的系统定义副本

#7


0  

In the example it is probably an attempt to generate the UINT_MAX value. The technique is possibly flawed for reasons already stated.

在该示例中,可能尝试生成UINT_MAX值。由于已经陈述的原因,该技术可能存在缺陷。

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.

但是,该表达式确实有合法的用途来生成一个位掩码,所有位都使用与字符宽度无关的文字常量设置;但这不是你的例子中使用它的方式。

#8


0  

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. Thus, ~0 will produce a bit pattern of all ones. 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). 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.

在您给出的示例中,整数0的位表示全为零。因此,~0将产生所有1的位模式。即使0是int,也是分配给maxPeriod的位模式~0(不是由所述位模式表示的int值)。由于maxPeriod是一个无符号整数,因此它被赋予了由~0(所有1的模式)表示的unsigned int值,这实际上是unsigned int在回绕到0之前可以存储的最高值。

#1


25  

~0 is the bitwise complement of 0, which is a number with all bits filled. For an unsigned 32-bit int, that's 0xffffffff. The exact number of fs will depend on the size of the value that you assign ~0 to.

〜0是0的按位补码,它是填充所有位的数字。对于无符号的32位int,即0xffffffff。 fs的确切数量取决于您指定的值的大小~0。

#2


17  

It's the one complement, which inverts all bits.

它是一个补码,它反转所有位。

 ~  0101 => 1010
 ~  0000 => 1111
 ~  1111 => 0000

#3


9  

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. Setting the default argument to

特别是,~0的值不必为-1,这可能是预期的值。将默认参数设置为

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中无效。

#4


4  

It's a binary complement function.

它是二进制补码函数。

Basically it means flip each bit.

基本上它意味着翻转每一位。

#5


4  

It is the bitwise complement of 0 which would be, in this example, an int with all the bits set to 1. If sizeof(int) is 4, then the number is 0xffffffff.

它是0的按位补码,在这个例子中,它是一个int,所有位都设置为1.如果sizeof(int)是4,那么数字是0xffffffff。

#6


3  

Basically, it's saying that maxPeriod has a default value of UINT_MAX. Rather than writing it as UINT_MAX, the author used his knowledge of complements to calculate the value.

基本上,它说maxPeriod的默认值为UINT_MAX。作者不是将其写成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. Since we are dealing with an int, in which 0 is represented with all zero bits (00000000). Adding one would give (00000001), adding one more would give (00000010), and one more would give (00000011). Finally one more addition would give (00000100) because the 1's carry.

现在解释为什么~0是UINT_MAX。因为我们正在处理一个int,其中0表示所有零位(00000000)。添加一个会给(00000001),再添加一个会给(00000010),还有一个会给(00000011)。最后再添加一个(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. This means that all one bits in an unsigned number is the maximum that data type (int in your case) can hold.

对于无符号整数,如果重复进程ad-infiniteum,最终你有一个位(11111111),并且添加另一个将溢出缓冲区设置所有位回零。这意味着无符号数中的所有位都是数据类型(在您的情况下为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. So he basically the previous coded opted to computer UINT_MAX instead of using the system defined copy located in #include <limits.h>

“〜”操作将所有位从0翻转为1或从1翻转为0,翻转零整数(具有全零位)有效地为您提供UINT_MAX。所以他基本上以前的编码选择了计算机UINT_MAX而不是使用#include 中的系统定义副本

#7


0  

In the example it is probably an attempt to generate the UINT_MAX value. The technique is possibly flawed for reasons already stated.

在该示例中,可能尝试生成UINT_MAX值。由于已经陈述的原因,该技术可能存在缺陷。

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.

但是,该表达式确实有合法的用途来生成一个位掩码,所有位都使用与字符宽度无关的文字常量设置;但这不是你的例子中使用它的方式。

#8


0  

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. Thus, ~0 will produce a bit pattern of all ones. 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). 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.

在您给出的示例中,整数0的位表示全为零。因此,~0将产生所有1的位模式。即使0是int,也是分配给maxPeriod的位模式~0(不是由所述位模式表示的int值)。由于maxPeriod是一个无符号整数,因此它被赋予了由~0(所有1的模式)表示的unsigned int值,这实际上是unsigned int在回绕到0之前可以存储的最高值。

相关文章