Java: what does ~ mean

In this Java source code, I have this line:

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

What does tilde ~ mean?

+55
java operators syntax special-characters
Sep 27 '09 at 12:35
source share
5 answers

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

See: Bitwise complement (~): inverts and zeros in number

+57
Sep 27 '09 at 12:39
source share

This is Unary ~ Bitwise addition operator (citation):

  • only used with integer values
  • inverts bits, i.e. 0-bit becomes 1-bit and vice versa
  • in all cases ~ x is equal to (-x) -1

See also this page on Bitwise Operators on wikipedia , which says:

Bitwise NOT or complement is a unary operation that performs logical negation on each bit, forming their complement to a given binary value. The numbers that were 0 become 1, and vice versa.
For example:

 NOT 0111 (decimal 7) = 1000 (decimal 8) 

In many programming languages โ€‹โ€‹(including the C family), the bitwise NOT operator is " ~ " (Tilda).

+38
Sep 27 '09 at 12:38
source share

As already mentioned, ~ is a unitary bitwise NOT operator.
In your example, it checks to see if the modifiers contain bits other than those defined in KeyEvent.SHIFT_MASK .

  • ~KeyEvent.SHIFT_MASK โ†’ all bits except those specified in KeyEvent.SHIFT_MASK are 1.
  • (modifiers & ~KeyEvent.SHIFT_MASK) every 1-bit in modifiers , which is not related to KeyEvent.SHIFT_MASK
  • if ((modifiers & ~KeyEvent.SHIFT_MASK) != 0) โ†’ if there was at least one bit set to 1, except for KeyEvent.SHIFT_MASK do something ...
+7
Sep 27 '09 at 12:58
source share

On the Java website http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html

The unary bitwise complement operator "~" inverts the bit pattern; This can be applied to any of the integral types, making each "0" a "1" and each "1" a "0". For example, a byte contains 8 bits; applying this operator to a value whose bit-bit is โ€œ00000000โ€, its pattern โ€œ11111111โ€ will change.

Now, as Pascal MARTIN previously answered, in any case, the vlaue value is - (x) -1. For example. ~ 2 = -3, ~ -6 = 5, etc.

In addition, in java, all positive integers are stored as their binary representations, and negative integers are stored in 2 compliment values โ€‹โ€‹of a positive integer.

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

Initially, 2 is stored in binary representation:

 0000 0000 0000 0010 

Now ~ 2 will result in a value (inverts bits):

 1111 1111 1111 1101 

How in the world do I know this is -3? Well, this is -3 because it is derived from 2 complimentary representations of 3.

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

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

Therefore, x = 1 compliment of the value (since the answer we received is 1 compliment of x).
x = 0000 0000 0000 0011 . So, we found that x is 3, and therefore our previous result of the ~ t25> -3 operator is written as 2 compliments of 3.

+4
Feb 18 '17 at 5:30
source share

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

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

+2
Sep 27 '09 at 12:42
source share



All Articles