int max = ~0; What does it mean?

前端 未结 8 2061
灰色年华
灰色年华 2021-01-19 03:46

int max = ~0;

What does it mean?

8条回答
  •  温柔的废话
    2021-01-19 04:45

    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.

提交回复
热议问题