Java Bitwise Operators Notes
ICSE Class 9 & 10 Computer Applications
1. Introduction to Bitwise Operators
Bitwise operators are special operators in Java that work directly on binary bits (0 and 1) of
integer values.
They perform operations bit by bit.
Example:
5 = 0101
3 = 0011
Bitwise operators compare corresponding bits and produce a result.
2. Why Do We Use Bitwise Operators?
Bitwise operators are used for:
• Fast calculations
• Data manipulation
• Memory-efficient programming
• Binary operations
• Encryption and graphics programming
• Competitive programming
For ICSE Class 9 & 10, focus mainly on:
• Understanding binary operations
• Tracing outputs
• Simple programs
3. Binary Number Basics
Before learning bitwise operators, understand binary numbers.
Decimal Binary
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
4. Types of Bitwise Operators in Java
Operator Name
& Bitwise AND
` `
^ Bitwise XOR
~ Bitwise NOT
<< Left Shift
>> Right Shift
5. Bitwise AND Operator (&)
The AND operator returns 1 only when both bits are 1.
Truth Table
AB A&B
00 0
01 0
10 0
11 1
Example
5&3
Binary form:
5 = 0101
3 = 0011
--------------
0001
Result = 1
Java Program
class AndDemo
{
public static void main()
{
int a = 5;
int b = 3;
int c = a & b;
[Link]("Result = " + c);
}
}
Output:
Result = 1
6. Bitwise OR Operator (|)
The OR operator returns 1 if at least one bit is 1.
Truth Table
AB A|B
00 0
01 1
10 1
11 1
Example
5|3
Binary:
5 = 0101
3 = 0011
--------------
0111
Result = 7
Java Program
class OrDemo
{
public static void main()
{
int a = 5;
int b = 3;
[Link](a | b);
}
}
Output:
7. Bitwise XOR Operator (^)
XOR returns 1 when both bits are different.
Truth Table
AB A^B
00 0
01 1
10 1
11 0
Example
5^3
Binary:
5 = 0101
3 = 0011
--------------
0110
Result = 6
Java Program
class XorDemo
{
public static void main()
{
int a = 5;
int b = 3;
[Link](a ^ b);
}
}
Output:
8. Bitwise NOT Operator (~)
The NOT operator reverses all bits.
• 1 becomes 0
• 0 becomes 1
Example
~5
Binary of 5:
00000101
After NOT:
11111010
Result = -6
Important Concept
Java uses 2’s complement representation for negative numbers.
Shortcut formula:
~n = -(n + 1)
Example:
~5 = -(5 + 1)
= -6
Java Program
class NotDemo
{
public static void main()
{
int a = 5;
[Link](~a);
}
}
Output:
-6
9. Left Shift Operator (<<)
Left shift moves bits to the left.
Each left shift multiplies the number by 2.
Example
5 << 1
Binary:
5 = 00000101
After shifting left by 1:
00001010
Result = 10
Shortcut Rule
n << x = n × 2^x
Example:
5 << 1 = 5 × 2 = 10
5 << 2 = 5 × 4 = 20
Java Program
class LeftShift
{
public static void main()
{
int a = 5;
[Link](a << 1);
}
}
Output:
10
10. Right Shift Operator (>>)
Right shift moves bits to the right.
Each right shift divides the number by 2.
Example
20 >> 1
Binary:
20 = 00010100
After shifting right:
00001010
Result = 10
Shortcut Rule
n >> x = n ÷ 2^x
Example:
20 >> 1 = 10
20 >> 2 = 5
Java Program
class RightShift
{
public static void main()
{
int a = 20;
[Link](a >> 1);
}
}
Output:
10
11. Important Differences
Operator Main Use
& Checks if both bits are 1
` `
^ Checks if bits are different
~ Reverses bits
<< Multiply by powers of 2
Operator Main Use
>> Divide by powers of 2
12. Most Important ICSE Exam Points
Remember These Rules
AND (&)
• 1 only if both are 1
OR (|)
• 0 only if both are 0
XOR (^)
• 1 only if bits differ
NOT (~)
• Formula:
~n = -(n + 1)
Left Shift
n << x = n × 2^x
Right Shift
n >> x = n ÷ 2^x
13. Common Output Questions
Example 1
[Link](6 & 4);
Binary:
6 = 0110
4 = 0100
---------
0100
Output:
Example 2
[Link](7 | 2);
Output:
Example 3
[Link](8 >> 2);
Output:
Example 4
[Link](3 << 3);
Output:
24
14. Advantages of Bitwise Operators
• Faster execution
• Efficient memory usage
• Useful in low-level programming
• Helps in binary calculations
15. Disadvantages
• Harder to understand
• Mostly used in advanced programming
• Can confuse beginners
16. Viva / Oral Questions
1. What are bitwise operators?
2. Which operator is used for bitwise AND?
3. What is the result of 5 & 3?
4. What does XOR mean?
5. Why does ~5 give -6?
6. What is left shift used for?
7. Difference between & and &&?
8. Difference between | and ||?
17. Difference Between Bitwise and Logical Operators
Bitwise Operator | Logical Operator
Works on bits | Works on conditions
&, ` | `
Used with integers | Used with boolean values
18. Practice Questions
Find the Output
1.
[Link](5 & 2);
2.
[Link](5 | 2);
3.
[Link](5 ^ 2);
4.
[Link](~7);
5.
[Link](4 << 2);
6.
[Link](16 >> 3);
19. Summary
Bitwise operators work directly on binary bits.
They are:
• & → AND
• | → OR
• ^ → XOR
• ~ → NOT
• << → Left Shift
• >> → Right Shift
Understanding binary conversion is the key to mastering bitwise operators.