问题
int max = ~0;
What does it mean?
回答1:
The ~
operator is the unary bitwise complement operator which computes the bitwise complement. This means that it reverses all the bits in its argument (0s become 1s and 1s become 0s). Thus,
int max = ~0;
which is setting max
to the negation of the 32-bit value 0000 0000 0000 0000 0000 0000 0000 0000
resulting in 1111 1111 1111 1111 1111 1111 1111 1111
. As we are storing this result in an Int32
, this is the same as -1
.
Whether or not it is better to say
int max = ~0;
or
int max = -1;
depends on the context. If the point of max
is to have a number all of whose bits are 1
I would choose the former. If the point of max
is to compute the maximum of a list of non-negative integers, I would choose the latter (well, I'd prefer int max = Int32.MinValue;
and even more so, I'd just prefer int max = list.Max();
).
回答2:
The ~ operator is a bit inverse, so ~0 gives you an integer value with all ones (in binary).
回答3:
It is a bitwise negation of the bytes 0000....0000. It is the value of an integer where all bits are set to 1.
In an unsigned situation it would be the maximum possible value. In a signed situation it is -1.
回答4:
~ is the complement operator, which flips the bits of the operand. Since zero has no bits set, the complement will have all bits set, which is also the maximum sized integer (assuming unsigned). For signed numbers, you're going to get -1 instead, so calling it "max" is a bit of a misnomer.
回答5:
~ means bitwise not, it inverts all the bits in the given integer. In a signed int this will give you -1 (since all the bits in the int will be flipped from 0 to 1.) Look up two's complement for more information on this one.
In an unsigned int (uint) this would give you the maximum value of an integer (since the most significant bit in an unsigned int doesn't determine the sign.)
回答6:
As noted, ~0 yields 0xFFFFFFFF.
However, I suspect the original programmer is confused. executing int max = ~0 ;
sets the signed integer max
to the value -1.
回答7:
Bitwise complement.
http://msdn.microsoft.com/en-us/library/d2bd4x66.aspx
A literal 0 (as in the code above) is an int.
An int is a 32 bit binary value. The value 0 has all the bits set to 0.
The ~ operator is a bitwise compliment. i.e. I swaps all the bits.
As all the bits were 0 they are all turned into 1. So we have a 32 bit value
with all the bits set to 1.
C# sharp uses 2 compliment. Which encodes -1 in an int as all bits being 1
0000 0000 0000 0000 0000 0000 0000 0000 == 0
operator ~
1111 1111 1111 1111 1111 1111 1111 1111 == -1
So => ~0 == -1
回答8:
As others have stated, ~ is the bitwise negation operator. It will take all bits of the integer value and toggles 0 and 1 (0 -> 1 and 1 -> 0).
~0 equals to -1 for a signed integer or Int32.
Usually either ~0 or -1 is used as the "ALL inclusive" mask (asterisk) when you are implementing a layer-based filtering system of some kind where you use a "layerMask" argument which by default equals to -1 meaning that it will return anything (does not filter). The filter is indeed using a AND operation (valueToFilter & layerMask).
valueToFilter & -1 will always be non-zero if valueToFilter is also non-zero. Zero otherwise.
来源:https://stackoverflow.com/questions/4765201/int-max-0-what-does-it-mean