☕💰 The 1 MILLION question: what is that ~ SYMBOL in JAVA?
☕💰 The 1 MILLION question: what is that ~ SYMBOL in JAVA?
One tiny character. One surprisingly tricky Java result. 😈
🔸 TLDR
~ is Java's bitwise complement operator.
It flips every bit:
▪️ 0 becomes 1
▪️ 1 becomes 0
And because Java represents signed integers using two's complement:
~x == -x - 1
So yes:
int x = 2;
System.out.println(~x); // -3
Why -3? 🤯

🔸 WHAT ACTUALLY HAPPENS?
In 32-bit binary, 2 starts like this:
00000000 00000000 00000000 00000010
Apply ~:
11111111 11111111 11111111 11111101
That bit pattern represents:
-3
So:
System.out.println(~2); // -3
System.out.println(~0); // -1
System.out.println(~1); // -2
System.out.println(~-1); // 0
System.out.println(~-2); // 1
The shortcut is:
~x = -(x + 1)
🔸 WHERE IS ~ ACTUALLY USEFUL?
A classic use is clearing bits from a bit mask:
int READ = 0b001;
int WRITE = 0b010;
int permissions = READ | WRITE;
// Remove WRITE
permissions = permissions & ~WRITE;
~WRITE flips the mask, then & clears precisely that bit.
🔸 JAVA CERTIFICATION TRAP ⚠️
~ works on integral values, not float or double.
And smaller integral types undergo numeric promotion:
byte b = 2;
var result = ~b;
System.out.println(result); // -3
result is an int, not a byte.
🔸 TAKEAWAYS
▪️ ~ means bitwise complement.
▪️ It flips every 0 and 1.
▪️ For Java integers: ~x == -x - 1.
▪️ ~2 is therefore -3, not simply "negative 2".
▪️ It is especially useful with masks and flags.
▪️ byte, short and char are promoted to int when using ~.
Tiny operator. Big certification question. ☕🧠
#Java #Java25 #JavaCertification #OCPJava #Bitwise #Programming #SoftwareEngineering #JavaDeveloper #Coding
Go further with Java certification:
Java👇
Spring👇
SpringBook👇
JavaFullstackBook👇