Bit Manipulation Basics AND OR XOR NOT Left Right Shift in DSA C# - Time & Space Complexity
Bit manipulation operations are very fast and work directly on the binary form of numbers.
We want to understand how the time to perform these operations changes as the number size grows.
Analyze the time complexity of the following bit manipulation operations.
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 bitwise operations on two integers.
Bitwise operations work on each bit of the numbers.
- Primary operation: Processing each bit of the integer.
- How many times: Once per bit in the integer (usually 32 or 64 bits).
Each operation looks at every bit once, so the time grows with the number of bits.
| Input Size (bits) | Approx. Operations |
|---|---|
| 8 | 8 operations |
| 32 | 32 operations |
| 64 | 64 operations |
Pattern observation: The time grows linearly with the number of bits in the number.
Time Complexity: O(1)
This means bitwise operations take constant time because the number of bits is fixed by the system.
[X] Wrong: "Bitwise operations take longer as the number value grows."
[OK] Correct: The time depends on the number of bits, not the value size. The bit count is fixed (like 32 or 64 bits), so time stays constant.
Understanding bitwise operations and their constant time nature helps you write efficient low-level code and answer common interview questions confidently.
"What if we used a custom big integer type with thousands of bits? 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 DQuick Check:
AND = 1 only if both bits 1 [OK]
- Confusing AND with OR operator
- Thinking AND flips bits
- Assuming AND returns 1 if either bit is 1
x in C#?Solution
Step 1: Recall left shift operator syntax in C#
The left shift operator is<<and used asx << nto shift bits left by n places.Step 2: Identify correct syntax among options
x << 2; usesx << 2;which is correct. x >> 2; is right shift, C uses invalid operator, D is invalid syntax.Final Answer:
x << 2; -> Option BQuick Check:
Left shift syntax is x << n [OK]
- Using >>> which is invalid in C#
- Confusing left shift with right shift
- Adding extra symbols after shift operator
int a = 5; // binary 0101 int b = 3; // binary 0011 int c = a ^ b; Console.WriteLine(c);
Solution
Step 1: Convert numbers to binary and apply XOR
5 in binary is 0101, 3 is 0011. XOR (^) returns 1 if bits differ, else 0.Step 2: Calculate XOR bit by bit
0101 ^ 0011 = 0110 (binary), which is 6 in decimal.Final Answer:
6 -> Option CQuick Check:
5 ^ 3 = 6 [OK]
- Confusing XOR with OR or AND
- Calculating decimal sum instead of XOR
- Misreading binary values
num:int num = 12; int inverted = ~num(); Console.WriteLine(inverted);
Solution
Step 1: Check usage of variable
The code usesnumnum()which looks like a function call butnumis an integer variable, so parentheses are invalid.Step 2: Confirm bitwise NOT operator usage
The bitwise NOT operator~is correctly used before the variable, so no error there.Final Answer:
Using parentheses after variable name num() -> Option AQuick Check:
Variables don't use () [OK]
- Thinking ~num() is valid syntax
- Assuming missing semicolon error
- Believing Console.WriteLine can't print integers
n = 8, which expression correctly checks if n is a power of two using bit manipulation in C#?Solution
Step 1: Understand the power of two property
A number is a power of two if it has exactly one bit set to 1 in binary.Step 2: Apply bitwise trick
For such numbers,n & (n - 1)clears the lowest set bit. If result is 0,nwas a power of two.Final Answer:
(n & (n - 1)) == 0 -> Option AQuick Check:
Power of two check uses n & (n-1) == 0 [OK]
- Using OR or XOR instead of AND
- Forgetting to compare result to zero
- Misunderstanding bitwise NOT usage
Ready to prove it?
Pass the test and this pattern moves unknown
