Amir
Amir

Reputation: 1979

What does ~0 mean in this code?

What's the meaning of ~0 in this code?
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;
}

Upvotes: 20

Views: 13305

Answers (8)

Edwin Buck
Edwin Buck

Reputation: 70909

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.

If you want to make the code a bit more readable in the future, include

#include <limits>

and change the call to 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.

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.

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>

Upvotes: 4

mocybin
mocybin

Reputation: 1027

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).

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.

Upvotes: 0

Clifford
Clifford

Reputation: 93476

In the example it is probably an attempt to generate the UINT_MAX value. 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.

Upvotes: 0

avakar
avakar

Reputation: 32635

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

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)).

Also note that default arguments are not valid in C.

Upvotes: 9

JSBձոգչ
JSBձոգչ

Reputation: 41378

~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.

Upvotes: 27

stacker
stacker

Reputation: 68962

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

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

Upvotes: 19

plinth
plinth

Reputation: 49189

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.

Upvotes: 4

Alexander Pogrebnyak
Alexander Pogrebnyak

Reputation: 45576

It's a binary complement function.

Basically it means flip each bit.

Upvotes: 4

Related Questions