Int max = ~ 0; What does it mean?

int max = ~ 0;

What does it mean?

+5
source share
8 answers

An operator ~is a unitary bitwise complement operator that computes a bitwise complement. This means that it changes all the bits in its argument (0s become 1s and 1s become 0s). In this way,

int max = ~0;

which sets maxthe negation of the 32-bit value 0000 0000 0000 0000 0000 0000 0000 0000, which results in 1111 1111 1111 1111 1111 1111 1111 1111. Since we store this result in Int32, this is the same as -1.

Is it better to talk

int max = ~0;

or

int max = -1;

. max , 1, . max , (, int max = Int32.MinValue; , int max = list.Max();).

+8

~ , ~ 0 ( ).

+5

0000.... 0000. , 1.

. -1.

+5

~ - , . , , ( , ). -1 , "max" .

+4

.
http://msdn.microsoft.com/en-us/library/d2bd4x66.aspx

0 ( ) - int.
Int - 32- . 0 , 0.

~ . .. . 0, 1. , 32-
, 1.

# sharp 2 . -1 int, 1

0000 0000 0000 0000 0000 0000 0000 0000   == 0

operator ~

1111 1111 1111 1111 1111 1111 1111 1111   == -1

So = > ~0 == -1

+3

~ , . int -1 ( int 0 1.) .

unsigned int (uint) ( unsigned int .)

+3

, ~ 0 0xFFFFFFFF.

, , . int max = ~0 ; max -1. ​​

+3

, ~ - . 0 1 (0 → 1 1 → 0).

~ 0 -1 Int32.

Usually, the mask is "ALL inclusive" (asterisk) using either ~ 0 or -1 when you use some kind of layer-based filtering system, where you use the "layerMask" argument, which is -1 by default, which means that it will return something (does not filter). The filter does use the AND operation (valueToFilter and layerMask).

valueToFilter and -1 will always be nonzero if valueToFilter is also nonzero. Zero otherwise.

0
source

All Articles