Java: What does ~ mean
The Tilde (~
) performs a bitwise complement of a numerical value in Java.
See: Bitwise complement (~
): inverts ones and zeroes in a number
It is the Unary ~ Bitwise complement operator (quoting) :
- only used with integer values
- inverts the bits ie a 0-bit becomes 1-bit and vice versa
- in all cases ~x equals (-x)-1
See also this page on Bitwise operators on wikipedia, which states :
The bitwise NOT, or complement, is a unary operation that performs logical negation on each bit, forming the ones' complement of the given binary value. Digits which were 0 become 1, and vice versa.
For example:
NOT 0111 (decimal 7)
= 1000 (decimal 8)
In many programming languages (including those in the C family), the bitwise NOT operator is "
~
" (tilde).