4

I'm trying to port this C# code:

public static ulong WILDCARD_COLLISION_TYPE
{
    get
    {
        int parse = ~0;
        return (ulong)parse;
    }
}

If I understand correctly, doesn't the ~ symbol perform a bitwise complement so what is the point of doing ~0? and then returning it?

Garry Pettet
  • 8,096
  • 22
  • 65
  • 103
  • 4
    This will return a numeric value composed of 64 bits all set that could be used as a bit mask to test some kind of flag – Steve Feb 20 '16 at 13:37
  • It's equivalent of combersome `int parsed = unchecked((int) 0xFFFFFFFF);` you can put it just as `return 0xFFFFFFFFUL;` but it's easy to *miscount* the `F`s – Dmitry Bychenko Feb 20 '16 at 13:46

1 Answers1

8

From the ~ Operator documentation:

The ~ operator performs a bitwise complement operation on its operand, which has the effect of reversing each bit. Bitwise complement operators are predefined for int, uint, long, and ulong.

For example:

unsigned int i = ~0;

Result: Max number I can assign to i

and

signed int y = ~0;

Result: -1

so fore more info we can say that ~0 is just an int with all bits set to 1. When interpreted as unsigned this will be equivalent to UINT_MAX. When interpreted as signed this will be -1

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Ravi Sharma
  • 487
  • 2
  • 18