Binary Adder Simulator
Sum (binary)
1111
Carry out
0
How it works
A binary adder performs addition in base 2. A half adder adds two single bits (A and B), producing a Sum bit (A XOR B) and a Carry bit (A AND B). A full adder adds three bits (A, B, and carry-in), producing Sum and Carry-out. Chaining n full adders creates an n-bit ripple carry adder.
**Ripple carry vs. carry-lookahead** In a ripple carry adder, each stage must wait for the carry from the previous stage — carry propagates from LSB to MSB one stage at a time. For 64-bit addition, this is 64 gate delays. Carry-lookahead adders compute all carry values in parallel by pre-computing generate (G = A·B) and propagate (P = A+B) signals, reducing latency to O(log n) stages. Modern CPUs use carry-lookahead or prefix adders for high-speed arithmetic.
**Overflow detection** In n-bit signed integer addition, overflow occurs when adding two same-sign numbers produces an opposite-sign result. Detection: overflow = Carry_in_to_MSB XOR Carry_out_of_MSB. In unsigned addition, overflow = Carry_out_of_MSB. Processors set overflow and carry flags in status registers for this purpose.
**BCD adders** Binary-coded decimal (BCD) represents each decimal digit as 4 bits (0000–1001). BCD addition requires correction after binary addition: if the 4-bit sum exceeds 9, add 0110 (6) to produce the correct BCD digit plus carry. BCD is used in financial calculations and displays where decimal precision is required.
**Binary subtraction** Subtraction A - B = A + (-B). Two's complement negation: -B = ~B + 1 (invert all bits, add 1). An adder/subtractor circuit: XOR each B bit with a control line (0 for add, 1 for subtract — XOR acts as controlled inverter), then set the carry-in to the control line value. Single circuit handles both addition and subtraction.
Frequently Asked Questions
- Modern CPUs use carry-lookahead adders (CLA) or prefix adders (Kogge-Stone, Brent-Kung). These pre-compute generate (G = A AND B) and propagate (P = A XOR B) signals for all bit positions simultaneously, then compute carries in parallel. A 64-bit Kogge-Stone adder requires only O(log₂ 64) = 6 logic levels regardless of bit width, vs. 64 levels for ripple carry. Processor adders operate in 1–2 clock cycles at GHz speeds. For SIMD operations, multiple parallel adders process several pairs of numbers simultaneously (e.g., 8 independent 32-bit additions in one instruction).
- Two's complement represents negative numbers: -n = NOT(n) + 1. Range for N bits: -2^(N-1) to +2^(N-1)-1. For 8 bits: -128 to +127. Advantages: only one zero (not +0 and -0 as in sign-magnitude), addition and subtraction use the same hardware (no need to check signs before operating), and overflow detection is simple (XOR of carry-in and carry-out at MSB). Most processors use two's complement for all integer arithmetic. Recognizing two's complement: the MSB is the sign bit (1 = negative). To negate: flip all bits, add 1.
- Fixed-point represents fractions by treating some bits as fractional. For Q8.8 format (8 integer bits, 8 fractional bits): the binary point is between bit 8 and bit 7. Value = integer part + fractional bits/256. Addition works identically to integer addition — just maintain the position of the binary point. Example: 3.5 in Q8.8 = 0000 0011.1000 0000 = 0x0380. For multiplication: multiply as integers (result has twice the fractional bits), then right-shift to restore the original scale. Fixed-point arithmetic is used in DSPs, embedded systems, and audio processing where floating-point hardware is unavailable or too slow.
- Unsigned overflow: result exceeds max unsigned value (2^N - 1). Adding 255 + 1 in 8-bit unsigned = 256, which wraps to 0. The carry flag is set. Signed overflow: adding two positive numbers gives a negative result, or two negatives give positive. Adding 127 + 1 in 8-bit signed = -128 (wrong). Detection: overflow = carry_in to MSB XOR carry_out from MSB. Processors set separate carry (C) and overflow (V) flags. Programmers must check the appropriate flag: unsigned arithmetic checks carry; signed arithmetic checks overflow. Confusing them is a common source of subtle bugs.