Bit Manipulation: Interview Questions and Practice Problems
Bit manipulation is the act of algorithmically manipulating bits or other pieces of data shorter than a word. Computer programming tasks that require bit manipulation include low-level device control, error detection and correction algorithms, data compression, encryption algorithms, and optimization. For most other tasks, modern programming languages allow the programmer to work directly with abstractions instead of bits that represent those abstractions. Source code that does bit manipulation makes use of the bitwise operations: AND, OR, XOR, NOT, and bit shifts.
Bit manipulation, in some cases, can obviate or reduce the need to loop over a data structure and can give many-fold speed-ups, as bit manipulations are processed in parallel, but the code can become more difficult to write and maintain.
In this post, we will discuss a few such interesting bit manipulation hacks and interview questions:
- Bit Hacks — Part 1 (Basic)
- Bit Hacks — Part 2 (Playing with k’th bit)
- Bit Hacks — Part 3 (Playing with the rightmost set bit of a number)
- Bit Hacks — Part 4 (Playing with letters of the English alphabet)
- Bit Hacks — Part 5 (Find the absolute value of an integer without branching)
- Find the total number of bits needed to be flipped
- Brian Kernighan’s Algorithm to count set bits in an integer
- Round up to the next highest power of 2
- Round up to the previous power of 2
- Compute the parity of a number using a lookup table
- Count set bits using a lookup table
- Multiply 16-bit integers using an 8-bit multiplier
- Swap individual bits at a given position in an integer
- Check if a number is a power of 4 or not
- Check if a number is a power of 8 or not
- Reverse bits of an integer
- Swap two bits at a given position in an integer
- Print binary representation of a number
- Add binary representation of two integers
- Swap adjacent bits of a number
- Check if adjacent bits are set in the binary representation of a number
- Reverse bits of an integer using a lookup table
- Circular shift on the binary representation of an integer by `k` positions
- Find XOR of two numbers without using the XOR operator
- Print all distinct subsets of a given set
- Find the missing number in an array
- Find the missing number in an array without using any extra space
- Find the odd occurring element in an array in a single traversal
- Find two odd occurring elements in an array without using any extra space
- Find all odd occurring elements in an array having a limited range of elements
- Find the duplicate element in a limited range array
- Find two duplicate elements in a limited range array (using XOR)
- Find the missing number and duplicate elements in an array
- Check if binary representation of a number is palindrome or not
- Efficiently implement power function
- Find the odd occurring element in an array in logarithmic time
- Huffman Coding Compression Algorithm
- XOR Linked List — Overview and Implementation in C/C++
- Generate the power set of a given set
- Swap two numbers without using a third variable | 5 methods
- Find the square of a number without using the multiplication and division operator
- Perform division of two numbers without using division operator
- Generate 0 and 1 with 75% and 25% probability
- Determine if two integers are equal without using comparison and arithmetic operators
- Compute modulus division without division and modulo operator
- Single line expressions to swap two integers in Java
- Find minimum or maximum of two integers without using branching
- Conditionally negate a value without branching
- Solve a given set of problems without using multiplication or division operators
- Generate binary numbers between 1 to `n` using a queue
Thank you.