๐ง What Are Bitwise Operators?
๐ Definition
Bitwise operators perform operations bit by bit on integer data types (byte, short, int, long).
Java internally represents integers in binary format, and bitwise operators directly manipulate those binary values.
๐ List of Bitwise Operators in Java
| Operator | Name |
|---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise NOT (Complement) |
<< | Left Shift |
>> | Right Shift |
>>> | Unsigned Right Shift |
๐งฉ Binary Representation (Very Important)
๐ How Numbers Are Stored
Example:
5 โ 00000101
3 โ 00000011
Bitwise operators compare each corresponding bit.
๐ 1. Bitwise AND (&)
๐ Working Rule
- Result bit is
1only if both bits are 1 - Otherwise result is
0
๐งช Example
int a = 5; // 0101
int b = 3; // 0011
System.out.println(a & b);
๐ง Binary Calculation
0101
0011
----
0001 โ 1
โ Output:
1
๐ Practical Use
- Checking odd/even numbers
if ((n & 1) == 0)
System.out.println("Even");
๐ 2. Bitwise OR (|)
๐ Working Rule
- Result bit is
1if at least one bit is 1
๐งช Example
int a = 5;
int b = 3;
System.out.println(a | b);
๐ง Binary Calculation
0101
0011
----
0111 โ 7
โ Output:
7
๐ 3. Bitwise XOR (^)
๐ Working Rule
- Result bit is
1if bits are different - Result is
0if bits are same
๐งช Example
int a = 5;
int b = 3;
System.out.println(a ^ b);
๐ง Binary Calculation
0101
0011
----
0110 โ 6
โ Output:
6
โญ Important Property of XOR
x ^ x = 0
x ^ 0 = x
๐ Swapping Without Temp Variable
int x = 5, y = 7;
x = x ^ y;
y = x ^ y;
x = x ^ y;
๐ 4. Bitwise NOT (~)
๐ Working Rule
- Converts
1to0 - Converts
0to1
๐งช Example
int a = 5;
System.out.println(~a);
๐ง Binary Explanation (Twoโs Complement)
5 โ 00000101
~5 โ 11111010
This equals:
-6
โ Output:
-6
โ ๏ธ Common Confusion
Many expect ~5 = -5, but the correct result is -6.
โฌ
๏ธ 5. Left Shift Operator (<<)
๐ Working Rule
- Shifts bits to the left
- Rightmost bits are filled with
0 - Equivalent to multiplying by
2โฟ
๐งช Example
int a = 5;
System.out.println(a << 1);
๐ง Binary Calculation
0101 << 1 โ 1010 โ 10
โ Output:
10
๐ Mathematical Relation
a << n = a ร (2โฟ)
โก๏ธ 6. Right Shift Operator (>>)
๐ Working Rule
- Shifts bits to the right
- Leftmost bit is filled with sign bit
- Equivalent to dividing by
2โฟ
๐งช Example
int a = 10;
System.out.println(a >> 1);
โ Output:
5
๐ With Negative Numbers
int a = -10;
System.out.println(a >> 1);
Sign bit is preserved.
โก๏ธโก๏ธ 7. Unsigned Right Shift (>>>)
๐ Working Rule
- Shifts bits right
- Always fills leftmost bits with
0 - Ignores sign bit
๐งช Example
int a = -10;
System.out.println(a >>> 1);
โ Produces a large positive number
๐ง Adding Two Numbers Using Bitwise Operators
๐ Logic Behind Addition
- XOR (
^) โ sum without carry - AND (
&) โ carry - Left shift carry
๐งช Program: Add Two Numbers Without +
int a = 5;
int b = 7;
while (b != 0) {
int carry = a & b;
a = a ^ b;
b = carry << 1;
}
System.out.println(a);
โ Output:
12
๐ง Why This Works
- XOR adds bits
- AND finds carry
- Left shift moves carry to correct position
- Repeats until no carry remains
This is how processors internally perform addition.
โ ๏ธ Common Mistakes with Bitwise Operators
โ Confusing Logical and Bitwise Operators
if (a && b) // ERROR
โ Correct:
if ((a & b) != 0)
โ Expecting Boolean Output
Bitwise operators return integers, not boolean values.
โ Using Bitwise Operators on Floating Types
double x = 5.0;
x & 3; // ERROR
Bitwise operators work only on integer types.
โ Misunderstanding ~
~x โ -x
Correct formula:
~x = -(x + 1)
๐ Summary of Bitwise Operators
&โ AND|โ OR^โ XOR~โ Complement<<โ Multiply by powers of 2>>โ Divide by powers of 2>>>โ Unsigned shift
๐ฏ Why Bitwise Operators Are Required
โญ Key Reasons
- Faster execution
- Low-level memory control
- Efficient calculations
- Used in cryptography, compression, OS design
- Common in competitive programming
๐ Conclusion
๐ Final Summary
Bitwise operators allow Java programs to work directly at the binary level, providing powerful and efficient ways to manipulate data. While they may seem complex at first, understanding their binary behavior makes them predictable and extremely useful. A strong grasp of bitwise operators not only improves programming skills but also deepens understanding of how computers work internally

