A binary number is a number expressed in the binary numeral system or base-2 numeral system which represents numeric values using two different symbols: typically 0 (zero) and 1 (one). The base-2 system is a positional notation with a radix of 2. Because of its straightforward implementation in digital electronic circuitry using logic gates, the binary system is used internally by almost all modern computers and computer-based devices. Each digit is referred to as a bit.
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)Easy
- Bit Hacks – Part 2 (Playing with k’th bit)Easy
- Bit Hacks – Part 3 (Playing with the rightmost set bit of a number)Easy
- Bit Hacks – Part 4 (Playing with letters of the English alphabet)Easy
- Bit Hacks – Part 5 (Find the absolute value of an integer without branching)Easy
- Find the total number of bits needed to be flippedEasy
- Brian Kernighan’s Algorithm to count set bits in an integerEasy
- Round up to the next highest power of 2Medium
- Round up to the previous power of 2Medium
- Compute the parity of a number using a lookup tableHard
- Count set bits using a lookup tableHard
- Multiply 16-bit integers using an 8-bit multiplierMedium
- Swap individual bits at a given position in an integerHard
- Check if a number is a power of 4 or notMedium
- Check if a number is a power of 8 or notMedium
- Reverse bits of an integerMedium
- Swap two bits at a given position in an integerMedium
- Print binary representation of a numberEasy
- Add binary representation of two integersEasy
- Swap adjacent bits of a numberMedium
- Check if adjacent bits are set in the binary representation of a numberEasy
- Reverse bits of an integer using a lookup tableHard
- Circular shift on the binary representation of an integer by
k
positionsMedium - Find XOR of two numbers without using the XOR operatorMedium
- Print all distinct subsets of a given setHard
- Find the missing number in an arrayEasy
- Find the missing number in an array without using any extra spaceEasy
- Find the odd occurring element in an array in a single traversalEasy
- Find two odd occurring elements in an array without using any extra spaceMedium
- Find all odd occurring elements in an array having a limited range of elementsMedium
- Find the duplicate element in a limited range arrayMedium
- Find two duplicate elements in a limited range array (using XOR)Medium
- Find the missing number and duplicate elements in an arrayMedium
- Check if binary representation of a number is palindrome or notEasy
- Efficiently implement power functionEasy
- Find the odd occurring element in an array in logarithmic timeMedium
- Huffman Coding Compression AlgorithmHard
- XOR Linked List – Overview and Implementation in C/C++Medium
- Generate the power set of a given setMedium
- Swap two numbers without using a third variable | 5 methodsEasy
- Find the square of a number without using the multiplication and division operatorEasy
- Perform division of two numbers without using division operatorMedium
- Generate 0 and 1 with 75% and 25% probabilityMedium
- Determine if two integers are equal without using comparison and arithmetic operatorsEasy
- Compute modulus division without division and modulo operatorEasy
- Single line expressions to swap two integers in JavaEasy
- Find minimum or maximum of two integers without using branchingHard
- Conditionally negate a value without branchingMedium
- Solve a given set of problems without using multiplication or division operatorsMedium
- Generate binary numbers between 1 to
n
using a queueEasy
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)