Bit Manipulation Basics AND OR XOR NOT Left Right Shift in DSA Javascript - Time & Space Complexity
Bit manipulation uses simple operations on numbers at the bit level.
We want to know how fast these operations run as input size changes.
Analyze the time complexity of the following code snippet.
const a = 5; // 0101 in binary
const b = 3; // 0011 in binary
const andResult = a & b; // AND operation
const orResult = a | b; // OR operation
const xorResult = a ^ b; // XOR operation
const notResult = ~a; // NOT operation
const leftShift = a << 1; // Left shift by 1
const rightShift = a >> 1; // Right shift by 1
This code performs basic bit operations on two numbers.
Each bit operation works on all bits of the number at once.
- Primary operation: Bitwise AND, OR, XOR, NOT, and shifts
- How many times: Once per operation, affecting all bits simultaneously
Operations process each bit in the number, so time grows with the number of bits.
| Input Size (bits) | Approx. Operations |
|---|---|
| 8 | 8 operations |
| 32 | 32 operations |
| 64 | 64 operations |
Pattern observation: Time grows linearly with the number of bits in the input.
Time Complexity: O(1)
This means bit operations run in constant time because the number of bits is fixed by the system.
[X] Wrong: "Bitwise operations take longer as numbers get bigger."
[OK] Correct: The number size is limited by fixed bits (like 32 or 64), so operations always take the same time.
Knowing bit operations run fast helps you choose them for quick checks or toggles in coding problems.
"What if we used big integers with thousands of bits? How would the time complexity change?"
Practice
5 and 3? (5 & 3)Solution
Step 1: Convert numbers to binary
5 in binary is 0101, 3 in binary is 0011.Step 2: Perform bitwise AND
0101 & 0011 = 0001 (only bits set in both numbers remain set).Final Answer:
1 -> Option AQuick Check:
5 & 3 = 1 [OK]
- Confusing AND with OR operation
- Not converting numbers to binary before operation
- Assuming decimal addition instead of bitwise
a and b in JavaScript?Solution
Step 1: Identify bitwise XOR operator in JavaScript
The XOR operator is represented by the caret symbol (^).Step 2: Check syntax correctness
let result = a ^ b; usesa ^ b, which is the correct syntax for bitwise XOR.Final Answer:
let result = a ^ b; -> Option DQuick Check:
XOR operator is ^ in JavaScript [OK]
- Using && which is logical AND, not bitwise
- Using | which is bitwise OR, not XOR
- Using % which is modulo, unrelated to bits
let x = 8; // binary 1000 let y = x >> 2; console.log(y);
Solution
Step 1: Understand right shift operation
Right shift by 2 means move bits 2 places to the right, dropping the rightmost bits.Step 2: Apply right shift on binary 1000
1000 >> 2 becomes 0010, which is decimal 2.Final Answer:
2 -> Option AQuick Check:
8 >> 2 = 2 [OK]
- Confusing right shift with left shift
- Not converting binary to decimal after shift
- Assuming shift adds zeros on left without dropping bits
n using bitwise NOT:let n = 5; let flipped = ~n; console.log(flipped);
What is the issue?
Solution
Step 1: Understand bitwise NOT in JavaScript
Bitwise NOT flips all bits and returns the two's complement negative number.Step 2: Explain why output is negative
For 5 (00000101), ~5 flips bits to 11111010 which is -6 in two's complement.Final Answer:
Bitwise NOT returns a negative number due to two's complement representation -> Option BQuick Check:
~n flips bits and returns negative number [OK]
- Expecting positive flipped bits as output
- Thinking ~ is unsupported operator
- Assuming input must be string for bitwise ops
num to 1 without changing other bits. Which expression correctly does this in JavaScript?Solution
Step 1: Understand setting a bit using OR and shift
Left shift 1 by 3 gives 00001000, which targets the 3rd bit.Step 2: Use OR to set the bit without changing others
OR with this mask sets the 3rd bit to 1, keeping others unchanged.Final Answer:
num = num | (1 << 3); -> Option CQuick Check:
Use OR with shifted 1 to set bit [OK]
- Using AND which clears bits instead of setting
- Using XOR which toggles bits and may unset
- Using left shift alone which changes number value
Ready to prove it?
Pass the test and this pattern moves unknown
