Bit Manipulation Basics AND OR XOR NOT Left Right Shift in DSA Java - Time & Space Complexity
Bit manipulation operations are basic steps computers use to handle numbers at the smallest level.
We want to know how the time to do these operations changes as the number size grows.
Analyze the time complexity of the following code snippet.
int a = 5; // 0101 in binary
int b = 9; // 1001 in binary
int andResult = a & b; // AND operation
int orResult = a | b; // OR operation
int xorResult = a ^ b; // XOR operation
int notResult = ~a; // NOT operation
int leftShift = a << 2; // Left shift by 2 bits
int rightShift = b >> 1; // Right shift by 1 bit
This code performs basic bit operations on two integers.
Each bit operation works on all bits of the number at once.
- Primary operation: Bitwise AND, OR, XOR, NOT, and shifts.
- How many times: Each operation touches every bit once.
As the number size grows (more bits), the operations take a bit longer because they process each bit.
| Input Size (bits) | Approx. Operations |
|---|---|
| 8 | 8 steps |
| 32 | 32 steps |
| 64 | 64 steps |
Pattern observation: The time grows linearly with the number of bits.
Time Complexity: O(n)
This means the time to do bit operations grows linearly with the number of bits in the number.
[X] Wrong: "Bit operations always take constant time no matter the number size."
[OK] Correct: Actually, the time depends on how many bits the number has, so bigger numbers take more steps.
Understanding bit operations and their time helps you write fast code and answer questions about low-level data handling.
"What if we used 128-bit numbers instead of 32-bit? How would the time complexity change?"
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
