Java: What does ~ mean

JavaSyntaxOperatorsSpecial Characters

Java Problem Overview


In this Java source code I have this line:

if ((modifiers & ~KeyEvent.SHIFT_MASK) != 0) ....

What does the tilde ~ mean?

Java Solutions


Solution 1 - Java

The Tilde (~) performs a bitwise complement of a numerical value in Java.

See: Bitwise complement (~): inverts ones and zeroes in a number

Solution 2 - Java

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).

Solution 3 - Java

From Java's website http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html

> The unary bitwise complement operator "~" inverts a bit pattern; it > can be applied to any of the integral types, making every "0" a "1" > and every "1" a "0". For example, a byte contains 8 bits; applying > this operator to a value whose bit pattern is "00000000" would change > its pattern to "11111111".

Now, as previously answered by Pascal MARTIN, at any given case the value equals to -(x)-1. E.g. ~2=-3, ~-6=5, etc.

Also, in java all positive integers are stored as their binary representations and negative integers are stored in 2's compliment value of a positive integer.

Now, let's see how it works in bit level in case of ~2=-3:

Initially, 2 is stored in its binary representation:

0000 0000 0000 0010

Now ~2 will result in the value (inverse the bits):

1111 1111 1111 1101

How in the world I know it is -3? Well, it is -3 because it is derived from 2's compliment representation of 3.

As we know 2's(x)= 1's(x) + 1 (https://delightlylinux.wordpress.com/2014/10/13/binary-lesson-12-ones-complement-and-twos-complement/)
Our aim is it to find x:
1's(x)= 2's(x) - 1 (based on previous expression)

As our answer is in is in 2's compliment,
1's(x)= 1111 1111 1111 1101 - 0000 0000 0000 0001
1's (x)= 1111 1111 1111 1100 (How to subtract -http://sandbox.mc.edu/~bennet/cs110/pm/sub.html)

Therefore x= 1's compliment of value (as the answer we got represents 1's compliment of x).
x = 0000 0000 0000 0011
So, we have found that x is 3 and hence our previous result of ~ operator 1111 1111 1111 1101is -3 written as 2's compliment of 3.

Solution 4 - Java

As said before ~ is the unary bitwise NOT operator.
Your example tests whether modifiers contains bits other than those defined in KeyEvent.SHIFT_MASK.

  • ~KeyEvent.SHIFT_MASK -> all bits except those in KeyEvent.SHIFT_MASK are set to 1.
  • (modifiers & ~KeyEvent.SHIFT_MASK) -> every 1-bit in modifiers that "does not belong" to KeyEvent.SHIFT_MASK
  • if ((modifiers & ~KeyEvent.SHIFT_MASK) != 0) -> if there was at least one other bit set to 1 besides KeyEvent.SHIFT_MASK do something...

Solution 5 - Java

From the official docs http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op3.html:

> The unary bitwise complement operator "~" inverts a bit pattern; it can be applied to any of the integral types, making every "0" a "1" and every "1" a "0". For example, a byte contains 8 bits; applying this operator to a value whose bit pattern is "00000000" would change its pattern to "11111111".

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMartijn CourteauxView Question on Stackoverflow
Solution 1 - JavaRichardView Answer on Stackoverflow
Solution 2 - JavaPascal MARTINView Answer on Stackoverflow
Solution 3 - JavaDhwaneelView Answer on Stackoverflow
Solution 4 - JavaVolkerKView Answer on Stackoverflow
Solution 5 - JavaAlberto ZaccagniView Answer on Stackoverflow