Bit-manipulation

26 articles

dsa5 min read

Bit Manipulation — Complete Guide

Master bit manipulation: XOR tricks, counting bits, bitmask DP, Brian Kernighan, two's complement, and 5-language operators.

Read →
dsa1 min read

Single Number III — XOR Partition

Two numbers appear once, rest appear twice. XOR all to get a^b, then use any set bit to partition numbers into two groups.

Read →
dsa1 min read

Reverse Bits — 32-Bit Reversal

Reverse bits of a 32-bit unsigned integer. Shift result left and input right 32 times, taking last bit each time.

Read →
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 →
dsa1 min read

Subsets — Bitmask Enumeration

Generate all subsets of an array. Each subset corresponds to a bitmask of n bits where bit i = 1 means element i is included.

Read →