Bitwise operators are one of the most interesting topics in Java because they work directly on the binary representation (bits) of numbers.
They're widely used in:
- Performance optimization
- Flags and permissions
- Encryption algorithms
- Networking
- Device drivers
- Competitive programming
- Interview questions
If bitwise operators have ever seemed confusing, this guide will make them easy to understand with simple binary examples.
What are Bitwise Operators?
Bitwise operators perform operations on the individual bits of a value instead of the entire number.
Java provides five bitwise-related operators:
| Operator | Name | Works On |
|---|---|---|
& |
Bitwise AND | boolean & Integral |
| |
Bitwise OR | boolean & Integral |
^ |
Bitwise XOR | boolean & Integral |
~ |
Bitwise Complement | Integral only |
! |
Logical NOT | boolean only |
The Three Main Bitwise Operators
| Operator | Name | Rule |
|---|---|---|
& |
AND | Result is 1 only if both bits are 1 |
| |
OR | Result is 1 if at least one bit is 1 |
^ |
XOR (Exclusive OR) | Result is 1 only when both bits are different |
Truth Table
For boolean values:
| A | B | A & B | A | B |
A ^ B |
|---|---|---|---|---|
| true | true | true | true | false |
| true | false | false | true | true |
| false | true | false | true | true |
| false | false | false | false | false |
Rule #1: &, | and ^ Work on Both boolean and Integral Types
This is one of the unique features of Java.
These operators can be used with:
- boolean
- byte
- short
- int
- long
- char
Example with Booleans
public class Main {
public static void main(String[] args) {
System.out.println(true & false);
System.out.println(true | false);
System.out.println(true ^ false);
}
}
Output
false
true
true
Example with Integers
public class Main {
public static void main(String[] args) {
System.out.println(4 & 5);
System.out.println(4 | 5);
System.out.println(4 ^ 5);
}
}
Output
4
5
1
How Bitwise Operators Work
Let's first convert the numbers into binary.
4 = 100
5 = 101
Bitwise AND (&)
100
& 101
-------
100
100₂ = 4
Result:
4
Only the bits that are 1 in both numbers remain 1.
Bitwise OR (|)
100
| 101
-------
101
101₂ = 5
Result:
5
A bit becomes 1 if either number has 1.
Bitwise XOR (^)
XOR means Exclusive OR.
A bit becomes 1 only when both bits are different.
100
^ 101
-------
001
001₂ = 1
Result:
1
More Integer Examples
Example 1
6 = 110
3 = 011
AND
110
011
---
010 = 2
OR
110
011
---
111 = 7
XOR
110
011
---
101 = 5
Java Code
System.out.println(6 & 3); // 2
System.out.println(6 | 3); // 7
System.out.println(6 ^ 3); // 5
Bitwise Complement (~)
The complement operator flips every bit.
0 → 1
1 → 0
Rule
~ works only on integral types.
Valid
System.out.println(~4);
Invalid
System.out.println(~true);
Compile-time Error
operator ~ cannot be applied to boolean
How ~4 Becomes -5
This is one of the most common interview questions.
Step 1
Represent 4 as a 32-bit integer.
00000000 00000000 00000000 00000100
Step 2
Flip every bit.
11111111 11111111 11111111 11111011
The leftmost bit is now 1, indicating a negative number in Java's two's complement representation.
Step 3
Find the Magnitude
Take the 2's complement:
Flip bits
00000000 00000000 00000000 00000100
Add 1
00000000 00000000 00000000 00000101
= 5
Therefore,
~4 = -5
Shortcut Formula
Instead of converting to binary every time, remember this simple formula:
~n = -(n + 1)
Examples:
| Expression | Result |
|---|---|
~4 |
-5 |
~0 |
-1 |
~10 |
-11 |
~-5 |
4 |
~-1 |
0 |
Verification:
~10
= -(10 + 1)
= -11
Sign Bit Explained
For Java integral types, the Most Significant Bit (MSB) is the sign bit.
0 → Positive
1 → Negative
Positive numbers are stored directly.
Negative numbers are stored in 2's complement form.
Boolean NOT Operator (!)
The ! operator is not a bitwise operator. It is the logical NOT operator and works only with boolean values.
System.out.println(!true);
System.out.println(!false);
Output
false
true
Invalid Example
System.out.println(!4);
Compile-time Error
operator ! cannot be applied to int
Difference Between ~ and !
| Operator | Works On |
|---|---|
~ |
Integral types only |
! |
boolean only |
A common mistake is trying to use ~ with booleans or ! with integers. Java does not allow this.
Common Examples
public class Main {
public static void main(String[] args) {
// Boolean
System.out.println(true & true);
System.out.println(true | false);
System.out.println(false ^ false);
// Integer
System.out.println(6 & 3);
System.out.println(6 | 3);
System.out.println(6 ^ 3);
// Complement
System.out.println(~10);
System.out.println(~-10);
// Logical NOT
System.out.println(!true);
}
}
Output
true
true
false
2
7
5
-11
9
false
Master Summary Table
| Operator | boolean | Integral Types |
|---|---|---|
& |
✅ | ✅ |
| |
✅ | ✅ |
^ |
✅ | ✅ |
~ |
❌ | ✅ |
! |
✅ | ❌ |
Interview Tips
Q1. Which bitwise operators work with boolean values?
✅ &, |, ^, !
❌ ~
Q2. Which operator flips every bit?
~
Q3. What is the shortcut for calculating ~n?
~n = -(n + 1)
Q4. Why does ~4 become -5?
Because ~ flips every bit, producing the two's complement representation of -5.
Q5. Can ! be used with integers?
No.
!10
Results in a compile-time error.
Memory Tricks 🧠
& (AND)
All bits must be 1.
| (OR)
At least one bit must be 1.
^ (XOR)
Different bits produce 1.
~ (Complement)
Flip every bit.
Remember:
~n = -(n + 1)
! (Logical NOT)
Reverse the boolean value.
true → false
false → true
Key Takeaways
- Bitwise operators work directly on the binary representation of values.
-
&,|, and^work with both boolean and integral types. -
~works only with integral types and flips every bit. -
!works only with boolean values and performs logical negation. - The shortcut
~n = -(n + 1)helps compute complement results quickly. - Understanding binary makes bitwise operators much easier to use and is essential for Java interviews.
Happy Coding!
Top comments (0)