Content is user-generated and unverified.

Sum two integers in Brainfuck

Problem

Read two signed integers a and b separated by whitespace. Output their sum a + b as a plain decimal integer.

  • Input: 5 7 → Output: 12
  • Input: -3 10 → Output: 7
  • Input: -5 -3 → Output: -8

Constraints

Brainfuck cells are 8-bit unsigned (0–255), so the program uses two's complement internally to handle signed arithmetic. Valid operand range is roughly −128 to 127; the sum must also fit in that window.

EOF portability

BF interpreters disagree on what , does at end-of-input:

  • Some set the cell to 0
  • Some leave the cell unchanged
  • Some block, waiting for more input

This program clears the cell ([-]) before every , read. That way "leave unchanged" interpreters see 0 on EOF — identical to the "set to 0" family. If your interpreter blocks on EOF, make sure the input ends with a newline or send an explicit EOF signal (Ctrl-D on Unix, Ctrl-Z on Windows).

Strategy

The program works in four phases:

  • Parse each number from ASCII: handle an optional - prefix, then accumulate decimal digits via value = value × 10 + digit
  • Negate either value whose sign flag is set (convert sign-magnitude to 8-bit two's complement)
  • Add the two bytes — unsigned addition mod 256 is two's complement addition
  • Output the result: if the high bit is set (≥ 128) print - and negate back; then extract hundreds / tens / ones digits via repeated divmod by 10 and print, suppressing leading zeros

Memory layout

text
  Cell  Purpose (input phase)       Purpose (output phase)
  ───── ─────────────────────────── ────────────────────────
   0    value of number 1 (|a|)     two's complement result
   1    sign flag of number 1       (free)
   2    value of number 2 (|b|)     (free)
   3    sign flag of number 2       (free)
   4    current input character      (free)
   5–9  temporaries (digit check)   (free)
  10–19 work cells (addition)       (free)
  20    magnitude for output        ones digit
  21    quotient (divmod)           tens digit
  22    divmod counter              (free)
  23–30 temps (divmod, digit out)   digit extraction temps

Key subroutines

Multiply by 10

text
[>+<-]>[<++++++++++>-]<

Move cell to neighbour, then add 10 per count back into original.

Digit detection (is char − 48 < 10?)

Race a copy of (char − 48) against a counter initialised to 10. Decrement both each iteration; when the counter hits zero first the value was ≥ 10 (not a digit) and the loop is force-exited. If the value hits zero first (counter still positive), it was a valid digit.

Divmod by 10

A countdown counter starts at 10 and is decremented in lockstep with the value. Every time the counter reaches zero, it resets to 10 and the quotient increments. After the value is exhausted the remainder is recovered as 10 − counter.

Two's complement negation

text
[>+<-]>[<->-]<

Move value to temp; then for each count in temp, decrement the original cell. Since the cell starts at 0 this computes 0 − value (mod 256).

Safe read (EOF-portable)

text
[-],

Clear cell before reading. On "leave unchanged" interpreters, EOF now yields 0 instead of the stale cell value — preventing infinite loops in the digit-parsing loop.

Raw code (2329 characters)

brainfuck
>>>>[-],[>+>+<<-]>>[<<+>>-]<------------------------------------------
--->+<[>-<[-]]>[<<<<<+>>>[-],>>-]<<[>+>+<<-]>>[<<+>>-]<---------------
--------------------------------->++++++++++<[->[>+>+<<-]>>[<<+>>-]+<[
<->>-<[-]]>[<<<[-]>>>-]<<<]>>>>[-]<<<[>>>+<<<-]<[-]>>>>[<<<<<<<<<[>>>>
>+<<<<<-]>>>>>[<<<<<++++++++++>>>>>-]<[>+>+<<-]>>[<<+>>-]<------------
------------------------------------[<<<<<+>>>>>-]<[-],[>+>+<<-]>>[<<+
>>-]<------------------------------------------------>++++++++++<[->[>
+>+<<-]>>[<<+>>-]+<[<->>-<[-]]>[<<<[-]>>>-]<<<]>>>>[-]<<<[>>>+<<<-]<[-
]>>>>]<<<<<[-]>[-]<[-],[>+>+<<-]>>[<<+>>-]<---------------------------
------------------>+<[>-<[-]]>[<<<+>[-],>>-]<<[>+>+<<-]>>[<<+>>-]<----
-------------------------------------------->++++++++++<[->[>+>+<<-]>>
[<<+>>-]+<[<->>-<[-]]>[<<<[-]>>>-]<<<]>>>>[-]<<<[>>>+<<<-]<[-]>>>>[<<<
<<<<[>>>+<<<-]>>>[<<<++++++++++>>>-]<[>+>+<<-]>>[<<+>>-]<-------------
-----------------------------------[<<<+>>>-]<[-],[>+>+<<-]>>[<<+>>-]<
------------------------------------------------>++++++++++<[->[>+>+<<
-]>>[<<+>>-]+<[<->>-<[-]]>[<<<[-]>>>-]<<<]>>>>[-]<<<[>>>+<<<-]<[-]>>>>
]<<<<<[-]>[-]<<<<[<[>>>>>>>>>>+<<<<<<<<<<-]>>>>>>>>>>[<<<<<<<<<<->>>>>
>>>>>-]<<<<<<<<<-]>>[<[>>>>>>>>+<<<<<<<<-]>>>>>>>>[<<<<<<<<->>>>>>>>-]
<<<<<<<-]<[<<+>>-]<<[>>>>>>>>>>>+>+<<<<<<<<<<<<-]>>>>>>>>>>>>[<<<<<<<<
<<<<+>>>>>>>>>>>>-]<<+++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++>[<[>>+>+<<<-]>>>[<<<+>>>-]+<[<<->->>-<[-]]>[<<[-]>>-]<<]>+<<
[>>-<<[-]]>>[>+++++++++++++++++++++++++++++++++++++++++++++.[-]<<<<<<<
<<<<<<[>>>>>>>>>>>>>+<<<<<<<<<<<<<-]>>>>>>>>>>>>>[<<<<<<<<<<<<<->>>>>>
>>>>>>>-]<-]<<<<<<<<<<<<[>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<-]>>
>>>>>>>>>>>>>>>>>>>[-]>[-]++++++++++<<[->>-[>+>+<<-]>>[<<+>>-]<[>+<[-]
]>>+<[>-<-]>[<<<<+>[-]++++++++++>>>-]<<<<<]++++++++++>>[<<->>-]>>>>>>[
-]<<<<<<[-]++++++++++<[->-[>+>+<<-]>>[<<+>>-]<[>+<[-]]>>+<[>-<-]>[>>>+
<<<<<<[-]++++++++++>>>-]<<<<]++++++++++>[<->-]>>>>>>[[>+<-]>++++++++++
++++++++++++++++++++++++++++++++++++++.[-]>+<<]<<<<<<<[>>+>+<<<-]>>>[<
<<+>>>-]<[>+<[-]]>>>>>>>[<<<<<<<+>>+>>>>>-]<<<<<[>>>>>+<<<<<-]<[<+>-]<
[>+<[-]]>[<<<[>>>>>>>>+<<<<<<<<-]>>>>>>>>+++++++++++++++++++++++++++++
+++++++++++++++++++.[-]>[-]+<<<<<<-]<<<<++++++++++++++++++++++++++++++
++++++++++++++++++.

Test results

text
Input        Expected  Actual  EOF=0  EOF=unch
───────────  ────────  ──────  ─────  ────────
5 7          12        12      OK     OK
-3 10        7         7       OK     OK
-5 -3        -8        -8      OK     OK
0 0          0         0       OK     OK
9 1          10        10      OK     OK
99 1         100       100     OK     OK
3 -3         0         0       OK     OK
-3 3         0         0       OK     OK
10 -3        7         7       OK     OK
0 -5         -5        -5      OK     OK
-5 0         -5        -5      OK     OK
100 27       127       127     OK     OK
50 50        100       100     OK     OK
-1 -1        -2        -2      OK     OK
64 63        127       127     OK     OK
-64 -63      -127      -127    OK     OK
-128 0       -128      -128    OK     OK
127 0        127       127     OK     OK
1 -1         0         0       OK     OK
-50 50       0         0       OK     OK

Overflow note

Because cells are 8 bits, sums outside −128 … 127 wrap around. For example 99 + 99 = 198, which exceeds 127 and prints −58 (= 198 − 256). This is consistent two's complement behaviour, not a bug.

Content is user-generated and unverified.
    Sum Two Integers in Brainfuck: Complete Guide | Claude