dsa1 min read
Sum of Two Integers — Bit Addition
Add two integers without + or - operators. Simulate: sum = a XOR b (no carry), carry = (a AND b) << 1. Repeat until carry = 0.
Read →
webcoderspeed.com
1276 articles
Add two integers without + or - operators. Simulate: sum = a XOR b (no carry), carry = (a AND b) << 1. Repeat until carry = 0.
Generate all subsets of an array. Each subset corresponds to a bitmask of n bits where bit i = 1 means element i is included.
Can array be partitioned into k equal-sum subsets? Bitmask DP: dp[mask] = sum filled so far. O(2^n * n) instead of O(k^n) DFS.
Sum of Hamming distances between all pairs. For each bit position, count 0s and 1s: contribution = count_0 * count_1.
AND of all numbers in [left, right]. Result is the common prefix of left and right: keep shifting right until equal, then shift back.