Pattern visualizer
Sum of Two Integers Without + or -
Column addition does two things at once: it writes a digit and it may push a carry to the next column left. Split them and the plus sign disappears. XOR writes the digit — it is 1 exactly when one of the two bits is 1, which is binary addition with the carry thrown away. AND finds the columns where both bits are 1, and those are precisely the columns that overflow, so shifting that pattern one place left is the carry. Feed the carry back in as the new b and repeat: each pass moves weight out of the carry and into the sum without changing the total, and once b reaches 0 there is nothing left to carry and a holds the answer. Animated on: a = 11, b = 7 — return their sum without using + or -. Cells are the six bit columns of a, MSB on the left; pointers mark the bits still set in b..
XOR adds the columns, AND-then-shift carries them
The cells are the bits of a = 11 (001011), MSB on the left; the pointers mark the bits still set in b = 7 (000111). Adding by hand is really two jobs — write the column digit, then push a carry one column left — and each job has its own bitwise operator, so neither needs a plus sign.
1FUNCTION getSum(a, b)2 WHILE b != 03 carry <- (a AND b) SHIFTED LEFT 14 a <- a XOR b5 b <- carry6 RETURN a
← / → step · space play · Home restart