Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: What does ~ mean

In this Java source code I have this line:

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

What does the tilde ~ mean?

like image 338
Martijn Courteaux Avatar asked Sep 27 '09 12:09

Martijn Courteaux


People also ask

What does ?= Mean in Java?

it means: if(min >= 2) someval =2; else someval =1. Its called a ternary operator See this java example too.

What does '!' Mean in Java?

It is called a 'NOT' operator. It can be used to convert false to true or vice versa. By using this operator, the logical state of an operand is reversed. In simple words, it inverts the value of a boolean. Read Also: What does \n and \t mean in Java.


2 Answers

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

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

like image 178
Richard Avatar answered Oct 02 '22 17:10

Richard


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

like image 21
Pascal MARTIN Avatar answered Oct 02 '22 18:10

Pascal MARTIN