Commonly Asked Data Structure Interview Questions on Bit Manipulation
Last Updated : 19 Jun, 2025
Bit manipulation is a powerful technique often used in technical interviews to optimize space and time complexities by directly working with the binary representations of numbers. It allows you to perform operations such as setting, clearing, or toggling individual bits, and efficiently checking conditions like even or odd numbers, divisibility, or power of two.
Questions on bit manipulation typically test your understanding of binary operations, shifts, and logical bitwise operations like AND, OR, XOR, and NOT. Mastering bit manipulation can significantly improve your problem-solving skills, especially when dealing with low-level operations, memory optimization, and algorithm efficiency.
Top Coding Interview Questions on Bit Manipulation
The following list of top coding problems covers a range of difficulty levels, from easy to hard, to help candidates prepare for interviews.
Top Problems on Bit Manipulation for Interviews
Theoretical Questions for Interviews on Bit Manipulation
1. What is the purpose of the bitwise AND operator?
The AND operator (&) returns 1 when both bits are 1; otherwise, it returns 0. It’s often used to check if a number is even or odd by performing n & 1
.
2. How can you check if a number is a power of two using bit manipulation?
A number is a power of two if n & (n - 1)
equals 0, and n > 0
. This works because powers of two have only one bit set to 1.
3. What does the XOR operator do and how is it useful?
XOR (^) returns 1 when the bits are different. It’s used to find the difference between two numbers, or to toggle bits, such as swapping two numbers without a temporary variable.
4. How can you count the number of 1's (set bits) in a number?
You can use n & (n - 1)
to repeatedly remove the rightmost set bit, and count the iterations. This is a common trick to count set bits efficiently.
5. What is the significance of the bitwise OR operator?
The OR operator (|) returns 1 if at least one bit is 1. It's used to set specific bits in a number.
6. How do you toggle a specific bit in a number?
To toggle a bit, use XOR with a mask. For example, n ^= (1 << k)
will toggle the k-th bit of n
.
7. What is the result of shifting a number to the left or right?
Left shifting (<<
) multiplies a number by powers of two, while right shifting (>>
) divides it by powers of two, with the right shift for signed numbers preserving the sign.
8. What is the bitwise NOT operator and what does it do?
The NOT operator (~) inverts all the bits of a number. It’s useful for flipping bits or calculating the complement of a number.
9. How would you swap two numbers using bitwise XOR?
You can swap two numbers without a temporary variable by doing
a = a ^ b
b = a ^ b
a = a ^ b
10. How can you check if a number is even or odd using bit manipulation?
To check if a number is odd, simply do n & 1
. If the result is 1, the number is odd; if it’s 0, it’s even.
11. How can you check if two numbers have opposite signs using bit manipulation?
Two numbers have opposite signs if the sign bit differs. Check using the expression (a ^ b) < 0
; this will be true if a
and b
have different signs.
12. How can you determine if a number is divisible by 3 using bit manipulation?
A number is divisible by 3 if the difference between the count of set bits at odd and even positions is a multiple of 3. This can be done efficiently with bit shifts and XOR operations.
Similar Reads
10 Most Important Data Structures For Coding Interviews Data structures are important for proficient and effective programming. In coding interviews, familiarity with common data structures and their operations is very important for solving problems efficiently and quickly. In this article, we will see the ten most critical data structures for coding int
5 min read
OYO Rooms Interview Experience for SDE-I | On-Campus 2020 OYO visited our campus in the month of November for the SDE-I profile. A total of 1651 students applied. Round 1 (Online Test): The online test was on the HackerEarth platform which consisted of 10 MCQs based on OS, DBMS, and Data Structures followed by 2 programming questions. Smart points collecti
4 min read
Top Data Structures That Every Programmer Must Know A Data Structure organizes and stores data in a computer so that we can perform operations on the data more efficiently. There are many diverse applications of data structures in Computer Science and Software Engineering. The use of data structures is most common in all computer programs and softwar
15+ min read
Commonly Asked Data Structure Interview Questions To excel in a Data Structure interview, a strong grasp of fundamental concepts is crucial. Data structures provide efficient ways to store, organize, and manipulate data, making them essential for solving complex problems in software development.Interviewers often test candidates on various data str
6 min read
Veritas Interview Experience | Set 3 (On-Campus Aptitude Questions) Veritas visited our college for campus hiring, 30th August, 2017 . The selection process included aptitude test and Personal Interview round. Aptitude Test consisted of 20 questions based on: 1) Bitwise shift operator. 2) Pointer basics. 3) Object oriented concepts based on C++. 4) Output based ques
2 min read
Commonly Asked Data Structure Interview Questions on Hashing Hashing is a technique to map data to fixed-size values using a hash function, often used for quick lookups, insertions, and deletions in applications like databases and caches. The core concept behind hashing is to map large data to smaller fixed-size values, typically integers, through a hash func
4 min read