Rebuilding a Number
The pages before this one inspect a number. This one constructs one, bit by bit. The loop shape is the same every time: peel the lowest bit off the input, decide what the output bit should be, shift it into place, repeat.
That shape is worth internalizing, because it is also how the hardware works. A full adder is exactly this loop with one extra piece of state - the carry.
Complement & Reverse
Flipping every bit within the number's own width is the tricky part: Python integers are conceptually infinite, so a bare ~n gives you a negative number, not a complement. Build an all-ones mask of the right width and XOR against that instead. Reversing mirrors bit positions rather than flipping values, and its greedy cousin asks for the cheapest way to reach a palindrome.
476. Number Complement
The complement flips every bit within the width of num. Build a mask of all 1s that is exactly as wide as num by left-shifting and ORing in a 1 until the mask covers num, then XOR: every bit of num is inverted and nothing above it is touched. This is the negate-bits trick.
- Time
- O(log N)
- The mask grows once per bit of
num. - Space
- O(1)
- Only the mask is stored.
1009. Complement of Base 10 Integer
Identical to 476. Number Complement. Grow a mask of all 1s exactly as wide as n, then XOR to flip every bit of n and leave higher bits untouched.
- Time
- O(log N)
- The mask grows once per bit of
n. - Space
- O(1)
- Only the mask is stored.
190. Reverse Bits
190Reverse Bits
Build the reversed value one bit at a time. Each iteration shifts the accumulator r left to make room, then ORs in the current least significant bit of n. Shifting n right exposes the next bit. After 32 passes the bit order is fully mirrored.
- Time
- O(1)
- Exactly 32 iterations, independent of the value.
- Space
- O(1)
- Only the accumulator is stored.
3750. Minimum Number of Flips to Reverse Binary String
Turning n into its own reverse costs one flip per position where the two differ. bin(n)[-1:1:-1] walks the binary digits backwards, stopping before the 0b prefix, so rev is the mirrored string of the same length as bin(n)[2:]. Zip the two and count the mismatches.
- Time
- O(log N)
- One pass over the
log Nbinary digits ofn. - Space
- O(log N)
- Two binary strings of that same length are materialized.
Encode & Decode
An encoding is a bijection between an index and a bit string. Find the pattern - usually "the binary of n + 1 with its leading 1 stripped" - and the answer is a slice, not a search. The reverse direction subtracts a chosen popcount from a value until nothing is left.
1256. Encode Number
1256Encode Number
The encodings group by length: "", then "0"/"1", then "00".."11", and so on - exactly the pattern you get by writing num + 1 in binary and dropping its leading 1.
bin(num + 1) produces a string like "0b1011". Slicing [3:] removes the "0b" prefix and the leading 1, leaving the encoded suffix.
- Time
- O(log N)
- Converting
num + 1to binary scans its bits. - Space
- O(log N)
- The output string grows with the number of bits.
2749. Minimum Operations to Make the Integer Zero
After k operations, the remaining value is x = num1 - num2 * k. Reaching zero using exactly k operations of "subtract a power of two" is possible only if x can be written as a sum of k powers of two - which requires x >= k (each power of two contributes at least 1) and x's set-bit count is at most k (the minimum number of powers of two needed is x.bit_count(), and any larger k can be reached by splitting a power of two into two smaller ones). Try k = 1, 2, 3, ... and return the first one that satisfies both conditions; if x ever drops below k, no larger k can work either.
- Time
- O(log(num1))
kgrows untilx = num1 - num2 * kdrops belowk, which happens within a range bounded by the bit width ofnum1;bit_count()on each candidate is O(1) for fixed-width integers.- Space
- O(1)
- Only the scalars
kandxare tracked.
Arithmetic Without Operators
Simulate what + does internally. a ^ b is the sum with all carries ignored; (a & b) << 1 is exactly those carries, shifted into the column they belong to. Loop until the carry is zero. Doing the same on strings is the schoolbook version of the identical circuit.
371. Sum of Two Integers
Simulate the addition a hardware adder would do, one bit position at a time. At bit i, pull the next bit off of both a and b with divmod(a, 2) / divmod(b, 2) (this also shifts a and b right by one for the next iteration). The digit of the sum at that position is a_bit ^ b_bit ^ carry - XOR is addition-without-carry. The new carry is a full-adder's carry-out: if there was no incoming carry, a carry is produced only when both bits are 1 (a_bit & b_bit); if there was an incoming carry, a carry is produced when either bit is 1 (a_bit | b_bit) - the incoming carry itself is enough to push a lone 1 bit over. Doing this for all 32 bits and OR-ing each digit into total at its shifted position reconstructs the full sum.
The last two lines exist because Python integers have no fixed width. total is built up as a 32-bit unsigned bit pattern, but if the true sum is negative, that bit pattern's top bit is 1 - which in two's complement means "negative" for a real 32-bit integer, but Python just sees it as a large positive number (up to 0xFFFFFFFF). total > 0x7FFFFFFF is exactly the test "is the top bit set", and ~(total ^ 0xFFFFFFFF) converts that bit pattern back into the negative Python integer it represents - see the Python-specific gotcha on why this reinterpretation step is necessary at all, and exactly what each piece of it does.
- Time
- O(1)
- Always exactly 32 iterations, regardless of the input values.
- Space
- O(1)
- A fixed handful of integer variables.
67. Add Binary
Pad the shorter string with leading zeros so a and b line up, then walk both from the rightmost bit to the leftmost, exactly like adding by hand. At each position, x ^ y ^ carry is the sum bit (XOR is addition-without-carry), and a new carry is produced whenever at least two of x, y, and the old carry are 1 ((carry + x + y) >= 2). Each digit is pushed to the front of a deque as it's produced, and any carry left over after the last position becomes a leading 1.
- Time
- O(N)
Nis the length of the longer string; one pass over the padded bits.- Space
- O(N)
- The padded strings and the result deque each hold
Ncharacters.