Bit Manipulation: Interview Questions to Practice

Theory Topics:
- Introduction and Shift Operators
- Remaining Bitwise Operators
- Check ith bit
- Flip ith bit
- Check odd-even and powers of 2
- No. of 1s
- Clear all bits from LSB
Questions:
- Output:
#include <iostream>
using namespace std;int main(){
int x = 2;
x = x << 1;
cout << x;
}
answer is 4
Explanation: left shift — number is multiplied by 2
2. Output:
#include <iostream>
using namespace std;int main(){
int x = -2;
x = x >> 1;
cout << x;
}
Answer is -1
Explanation: right shift — number is divided by 2
3. Output:
#include <iostream>
using namespace std;int main(){
if(~0 == 1) {
cout << "yes";
}
else {
cout << "no";
}
}
Answer is no
Explanation: 0–000000 then ~0–111111 which is not 000001
4. Output:
#include <iostream>
using namespace std;int main(){
int x = -1;
if(!0 == 1) {
cout << "yes";
}
else {
cout << "no";
}
}
Output is yes
Explanation: since ‘not zero’ includes all numbers except 0, which can be 1
5. Output:
#include <iostream>
using namespace std;int main(){
int y = 0;
if(1 | (y = 1)) {
cout << "y is " << y;
}
else {
cout << y;
}
}
answer is y is 1
Explanation: 1 is 000001, y=1 — y is 000001, then 1 | 1 is 000001 | 000001 that is 000001 which is 1
6. Output:
#include <iostream>
using namespace std;int main(){
int y = 1;
if(y & (y = 2)) {
cout << "true";
}
else {
cout << "false";
}
}
answer is true
Explanation: 1 & 2–000001 & 000010–result of & is 000000–0. Now, the condition becomes if(0). In c, a condition of the form of if(number) is always true, so the output is true.
7. Which bitwise operator is suitable for turning off a particular bit in a number?
answer & operator
Explanation: Say you want to turn off second bit of 2 or 000010. You will perform & operation of this number with 111101. Result (000000) will be the unsetting of this bit and all other bits will remain the same.
8. Which bitwise operator is suitable for turning on a particular bit in a number?
Answer: | operator
Explanation: Say you want to turn on second bit of 1or 000001. You will perform |operation of this number with 000010. Result (000011) will be the setting of 2nd bit and all other bits will remain the same.
9. Which bitwise operator is suitable for checking whether a particular bit is on or off? (&, |, ^ operators)
Answer: &, |, ^
10. Set ith bit
You are given two integers N and i. You need to make ith bit of binary representation of N to 1 and return the updated N.
Counting of bits start from 0 from right to left.
Input Format : Two integers N and i (separated by space)
Output Format : Updated N
Sample Input 1 : 4 1
Sample Output 1 : 6
Sample Input 2 : 4 4
Sample Output 2 :20
11. Unset ith bit
You are given two integers N and i. You need to make ith bit of binary representation of N to 0 and return the updated N.
Counting of bits start from 0 from right to left.
Input Format : Two integers N and i (separated by space)
Output Format : Updated N
Sample Input 1 : 7 2
Sample Output 1 : 3
Sample Input 2 : 12 1
Sample Output 2 : 12
13. Check power of 4
You are given an integer N. You need to check if N is power of 4 or not. Return true or false accordingly.
Note : Do it in O(1) time.Input Format :Integers N
Output Format :true or false
Sample Input 1 :4
Sample Output 1 :true
Sample Input 2 :8
Sample Output 2 :false
14. Find 1st set bit
You are given an integer N. You need to return an integer M, in which only one bit is set which at position of lowest set bit of N (from right to left).
Input Format :Integer N
Output Format :Integer
Sample Input 1 :7
Sample Output 1 :1
Sample Input 2 :12
Sample Output 2 :4
15. Turn off 1st set bit
You are given an integer Ni. You need to make first set bit of binary representation of N to 0 and return the updated N.
Counting of bits start from 0 from right to left.
Input Format :Integer N
Output Format :Updated N
Sample Input 1 :4
Sample Output 1 :0
Sample Input 2 :12
Sample Output 2 :8
16. Clear all bits from MSB
You are given two integers N and i. You need to clear all bits from MSB to ith bit (start i from right to left) and return the updated N.
Counting of bits start from 0 from right to left.
Input Format :Two integers N and i (separated by space)
Output Format :Updated N
Sample Input 1 :15 2
Sample Output 1 :6
Sample Output 1 Explanation :
We need to clear all bits from MSB to ith bit i.e. clear all bits except 0th and 1st.
Sample Input 2 :4 4
Sample Output 2 :20
17. Flip a Specific Bit
18. Number of Ones
19. Check Odd Even
20. Check Power of 2
21. Clear All Bits from LSB
