Check if K-th Bit is SetProblem StatementGiven a number n, check if the k-th bit (0-indexed) is set (1) or not (0).Solutiondef is_kth_bit_set(n, k): return (n & (1 << k)) != 0
Set K-th BitProblem StatementGiven a number n, set the k-th bit to 1.Solutiondef set_kth_bit(n, k): return n | (1 << k)
Clear K-th BitProblem StatementGiven a number n, clear the k-th bit (set it to 0).Solutiondef clear_kth_bit(n, k): return n & ~(1 << k)
Toggle K-th BitProblem StatementGiven a number n, toggle the k-th bit (0 to 1, or 1 to 0).Solutiondef toggle_kth_bit(n, k): return n ^ (1 << k)
Count Set BitsProblem StatementCount the number of 1s in the binary representation of a number.Solutiondef count_set_bits(n): count = 0 while n: n &= (n - 1) count += 1 return count
Check Power of TwoProblem StatementDetermine if a number is a power of two.Solutiondef is_power_of_two(n): return n > 0 and (n & (n - 1)) == 0
Find Single NumberProblem StatementGiven an array where every element appears twice except for one, find that single one.Solutiondef find_single_number(arr): res = 0 for num in arr: res ^= num return res
Swap Two NumbersProblem StatementSwap two numbers without using a temporary variable.Solutiondef swap_xor(a, b): a = a ^ b b = a ^ b a = a ^ b return a, b
Isolate Rightmost Set BitProblem StatementFind the number with only the rightmost set bit of n set.Solutiondef isolate_rightmost_bit(n): return n & -n
Binary to DecimalProblem StatementConvert a binary string to a decimal integer.Solutiondef binary_to_decimal(s): return int(s, 2)
Check if Number is Power of FourProblem StatementDetermine if a given integer is a power of four.Solutiondef is_power_of_four(n): return n > 0 and (n & (n - 1)) == 0 and (n & 0x55555555) != 0
Count Bits to Flip to Convert A to BProblem StatementFind the number of bits required to flip to convert integer A to integer B.Solutiondef bits_to_flip(a, b): diff = a ^ b count = 0 while diff: diff &= (diff - 1) count += 1 return count
Swap Odd and Even BitsProblem StatementSwap all even bits with odd bits in an integer (e.g., bit 0 with bit 1, bit 2 with bit 3).Solutiondef swap_bits(n): even_bits = n & 0xAAAAAAAA odd_bits = n & 0x55555555 return (even_bits >> 1) | (odd_bits << 1)
Find Absolute Value without BranchingProblem StatementCalculate the absolute value of an integer without using any if-statements or ternary operators.Solutiondef bitwise_abs(n): mask = n >> 31 return (n + mask) ^ mask
Check if Number is Multiple of 3Problem StatementCheck if a number is a multiple of 3 using bitwise operations.Solutiondef is_multiple_of_3(n): if n < 0: n = abs(n) if n == 0: return True if n == 3: return True odd_count = 0 even_count = 0 while n: if n & 1: odd_count += 1 n >>= 1 if n & 1: even_count += 1 n >>= 1 return is_multiple_of_3(abs(odd_count - even_count))
Reverse Bits of a 32-bit IntegerProblem StatementReverse the bits of a given 32-bit unsigned integer.Solutiondef reverse_bits(n): res = 0 for _ in range(32): res = (res << 1) | (n & 1) n >>= 1 return res
Find Next Higher Power of 2Problem StatementFind the smallest power of 2 that is greater than or equal to n.Solutiondef next_power_of_2(n): if n == 0: return 1 n -= 1 n |= n >> 1 n |= n >> 2 n |= n >> 4 n |= n >> 8 n |= n >> 16 return n + 1
Compute Modulo N without %Problem StatementCompute n % d where d is a power of 2 (d = 2^k).Solutiondef bitwise_mod(n, d): # Only works if d is a power of 2 return n & (d - 1)
Check if Two Integers have Opposite SignsProblem StatementDetermine if two integers have opposite signs without using branching.Solutiondef opposite_signs(x, y): return (x ^ y) < 0
Find Position of Rightmost Set BitProblem StatementFind the position of the only set bit in a number that is a power of 2.Solutionimport math def pos_rightmost_bit(n): if not (n > 0 and (n & (n - 1)) == 0): return -1 return int(math.log2(n)) + 1