Discover how tiny switches inside your computer can be flipped instantly to solve big problems fast!
Why Bit Manipulation Basics AND OR XOR NOT Left Right Shift in DSA Java?
Imagine you have a huge stack of papers, and you need to quickly find out which papers have a certain mark, combine marks from two stacks, or flip marks on papers. Doing this by checking each paper one by one is slow and tiring.
Manually checking or changing each paper's mark takes a lot of time and is easy to mess up. It's like counting hundreds of tiny details by hand, which is slow and full of mistakes.
Bit manipulation lets you handle many tiny yes/no marks all at once using simple commands. It's like having a magic tool that can quickly combine, flip, or shift all marks in one go, saving time and effort.
int countMarks(int[] papers) {
int count = 0;
for (int paper : papers) {
if (paper == 1) count++;
}
return count;
}int countMarks(int papers) {
int count = 0;
while (papers != 0) {
count += papers & 1;
papers >>= 1;
}
return count;
}Bit manipulation enables lightning-fast operations on data by working directly with the smallest building blocks of information.
In video games, bit manipulation helps quickly check which keys are pressed or which game features are active, making the game respond instantly.
Manual checking of bits is slow and error-prone.
Bit manipulation uses simple commands to handle many bits at once.
This makes programs faster and more efficient.
Practice
&) do when applied to two bits?Solution
Step 1: Understand bitwise AND operation
The AND operator compares each bit of two numbers and returns 1 only if both bits are 1.Step 2: Apply to example bits
For bits 1 & 1, result is 1; for 1 & 0 or 0 & 1 or 0 & 0, result is 0.Final Answer:
Returns 1 only if both bits are 1 -> Option CQuick Check:
AND = 1 only if both bits 1 [OK]
- Confusing AND with OR operator
- Thinking AND flips bits
- Assuming AND returns 1 if any bit is 1
x by 3 positions in Java?Solution
Step 1: Recall left shift operator syntax in Java
Left shift operator is<<and shifts bits to the left.Step 2: Identify correct usage
To shift variablexleft by 3 bits, usex << 3;. Other options are right shifts or invalid.Final Answer:
x << 3; -> Option AQuick Check:
Left shift = x << number [OK]
- Using >>> for left shift (it's unsigned right shift)
- Using <<< which is invalid syntax
- Confusing left and right shift operators
int a = 5; // binary 0101 int b = 3; // binary 0011 int c = a ^ b; System.out.println(c);
Solution
Step 1: Convert numbers to binary
a = 5 is 0101, b = 3 is 0011 in binary.Step 2: Apply XOR operation bitwise
XOR returns 1 if bits differ, 0 if same.
0101 XOR 0011 = 0110 (binary) which is 6 decimal.Final Answer:
6 -> Option DQuick Check:
5 ^ 3 = 6 [OK]
- Confusing XOR with OR or AND
- Calculating XOR incorrectly bitwise
- Assuming XOR adds numbers
int x = 8; int y = ~x << 2; System.out.println(y);
Solution
Step 1: Analyze bitwise NOT and shift usage
The NOT operator (~) flips all bits of x. Then the result is shifted left by 2 bits.Step 2: Check Java syntax and types
Both ~ and << operators work on int type. The expression is valid and prints an integer.Final Answer:
No error; the code runs and prints a valid integer -> Option AQuick Check:
~ and << valid on int [OK]
- Thinking ~ cannot be used on int
- Assuming operator order is wrong
- Believing int can't hold shifted values
n, which expression correctly toggles (flips) the 4th bit (counting from 0) of n using bitwise operators in Java?Solution
Step 1: Understand toggling a bit
Toggling means flipping the bit: 0 becomes 1, 1 becomes 0.Step 2: Identify correct operator for toggling
XOR (^) with 1 at the bit position flips that bit. Son ^ (1 << 4)toggles the 4th bit.Final Answer:
n = n ^ (1 << 4); -> Option BQuick Check:
Toggling bit uses XOR with mask [OK]
- Using OR to toggle (only sets bit)
- Using AND with negation to toggle (clears bit)
- Using NOT and shift incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
