Welcome to Bit Manipulation CodeSnap
Bit Manipulation problems test your understanding of how binary representations work at the lowest level. These types of challenges are common in system design, embedded development, and performance-critical applications. Mastery of this topic helps you write efficient, low-level code using bitwise operators like AND, OR, XOR, NOT, and bit shifts. You'll develop skills to solve problems involving parity, counting bits, and simulating arithmetic operations without traditional operators.
Table of Contents
Single Number
Find the element that appears only once in an array where every other element appears twice. Use XOR to cancel out pairs efficiently.
Number of 1 Bits
Count the number of '1' bits in the binary representation of an unsigned integer.
Power of Two
Check whether a given number is a power of two using bitwise properties.
Reverse Bits
Reverse the bits of a given 32-bit unsigned integer.
Missing Number
Find the missing number in a sequence using XOR and index sum logic.
Sum of Two Integers (Without + or -)
Calculate the sum of two integers without using the '+' or '-' operator by applying bitwise operations.
Bitwise AND of Numbers Range
Given a range [m, n], return the bitwise AND of all numbers in that range.
Count Bits
Return an array where each element is the number of 1 bits in its binary representation.
Find the Duplicate Number
Given an array containing n+1 integers where each integer is between 1 and n, find the duplicate using bitwise or binary search.
Maximum XOR of Two Numbers in an Array
Find two numbers in the array that result in the maximum XOR value. Use bit masking and trie data structure.
Hamming Distance
Calculate the Hamming distance between two integers — the number of differing bits.
Gray Code
Generate a Gray code sequence for a given number of bits. Each step in the sequence should differ by one bit.
Divide Two Integers
Implement division of two integers without using multiplication, division, and mod operators. Handle edge cases like overflow and negative numbers.