◄ WORLD V · SONNY 5DART 386 · a helldive at the net

BCD carry at ten, not sixteen — decimal arithmetic in a binary machine

Sometimes a machine must carry at ten, not sixteen — for money, for a display, for a calculator that must round in decimal. Binary-coded decimal stores each decimal digit in four bits, and after a binary add it adjusts: 9+1 gives 0x0A, which is not a valid decimal digit, so add 6 to force the decimal carry — 0x0A+6 = 0x10, the digit rolling to 0 and carrying 1 to the tens. The +6 is the whole trick (the gap between 16 and 10). The 6502 had a D flag for exactly this; it is the carry, taught to count in tens.

THE TECHNIQUE add binary, then +6 to force the carry at ten (the decimal adjust)

The demo adds 9+1 in BCD: the raw 0x0A is adjusted by +6 to 0x10 — decimal 10, carry to the tens: live demo


HISTORY & CREDIT BCD · the 6502 D flag / DAA

“Binary machines can only carry at powers of two.” — add 6 after a nibble add and the carry happens at ten; BCD makes a binary adder count in decimal. cited

the code · one decimal digit per 4 bits (0000–1001).
the adjust · after a binary add, +6 if the nibble exceeds 9 (or a half-carry) — forcing the carry at ten (DAA).
the flag · the 6502's D bit switches ADC/SBC into decimal mode.

Add in binary, nudge by six, and the carry lands at ten — decimal counting on a binary adder. The carry, in base ten. BCD / the D flag

RECOMMEND FOR I-13 the decimal carry, on the compiler

On the canonical compiler, BCD 9+1: raw 0x0A, plus the adjust 6 = 0x10 — tens digit 1, ones digit 0 (decimal 10):

$ i13 run c_bcd.i13 # 9 + 1, then +6 decimal adjust RUN OK · 18 step(s) · peak stack 2 · call depth 0 raw = 10 bcd = 16 (0x10) tens = 1 ones = 0 -- decimal 10: the carry landed at ten, not sixteen
Recommend: BCD is the carry taught to count in tens — the +6 adjust forcing a decimal carry — and i13 rolls 9+1 to 0x10. Not a keeper (a base adjustment is a correctness convention, and the 6502 D flag is a mode bit). The dart that shows the carry's radix is a choice: nudge by the gap between the base you have and the base you want.