Read two signed integers a and b separated by whitespace.
Output their sum a + b as a plain decimal integer.
5 7 → Output: 12-3 10 → Output: 7-5 -3 → Output: -8Brainfuck 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.
BF interpreters disagree on what , does at end-of-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).
The program works in four phases:
- prefix, then
accumulate decimal digits via value = value × 10 + digit- and
negate back; then extract hundreds / tens / ones digits via repeated
divmod by 10 and print, suppressing leading zeros 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[>+<-]>[<++++++++++>-]<Move cell to neighbour, then add 10 per count back into original.
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.
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.
[>+<-]>[<->-]<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).
[-],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.
>>>>[-],[>+>+<<-]>>[<<+>>-]<------------------------------------------
--->+<[>-<[-]]>[<<<<<+>>>[-],>>-]<<[>+>+<<-]>>[<<+>>-]<---------------
--------------------------------->++++++++++<[->[>+>+<<-]>>[<<+>>-]+<[
<->>-<[-]]>[<<<[-]>>>-]<<<]>>>>[-]<<<[>>>+<<<-]<[-]>>>>[<<<<<<<<<[>>>>
>+<<<<<-]>>>>>[<<<<<++++++++++>>>>>-]<[>+>+<<-]>>[<<+>>-]<------------
------------------------------------[<<<<<+>>>>>-]<[-],[>+>+<<-]>>[<<+
>>-]<------------------------------------------------>++++++++++<[->[>
+>+<<-]>>[<<+>>-]+<[<->>-<[-]]>[<<<[-]>>>-]<<<]>>>>[-]<<<[>>>+<<<-]<[-
]>>>>]<<<<<[-]>[-]<[-],[>+>+<<-]>>[<<+>>-]<---------------------------
------------------>+<[>-<[-]]>[<<<+>[-],>>-]<<[>+>+<<-]>>[<<+>>-]<----
-------------------------------------------->++++++++++<[->[>+>+<<-]>>
[<<+>>-]+<[<->>-<[-]]>[<<<[-]>>>-]<<<]>>>>[-]<<<[>>>+<<<-]<[-]>>>>[<<<
<<<<[>>>+<<<-]>>>[<<<++++++++++>>>-]<[>+>+<<-]>>[<<+>>-]<-------------
-----------------------------------[<<<+>>>-]<[-],[>+>+<<-]>>[<<+>>-]<
------------------------------------------------>++++++++++<[->[>+>+<<
-]>>[<<+>>-]+<[<->>-<[-]]>[<<<[-]>>>-]<<<]>>>>[-]<<<[>>>+<<<-]<[-]>>>>
]<<<<<[-]>[-]<<<<[<[>>>>>>>>>>+<<<<<<<<<<-]>>>>>>>>>>[<<<<<<<<<<->>>>>
>>>>>-]<<<<<<<<<-]>>[<[>>>>>>>>+<<<<<<<<-]>>>>>>>>[<<<<<<<<->>>>>>>>-]
<<<<<<<-]<[<<+>>-]<<[>>>>>>>>>>>+>+<<<<<<<<<<<<-]>>>>>>>>>>>>[<<<<<<<<
<<<<+>>>>>>>>>>>>-]<<+++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++>[<[>>+>+<<<-]>>>[<<<+>>>-]+<[<<->->>-<[-]]>[<<[-]>>-]<<]>+<<
[>>-<<[-]]>>[>+++++++++++++++++++++++++++++++++++++++++++++.[-]<<<<<<<
<<<<<<[>>>>>>>>>>>>>+<<<<<<<<<<<<<-]>>>>>>>>>>>>>[<<<<<<<<<<<<<->>>>>>
>>>>>>>-]<-]<<<<<<<<<<<<[>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<<<<-]>>
>>>>>>>>>>>>>>>>>>>[-]>[-]++++++++++<<[->>-[>+>+<<-]>>[<<+>>-]<[>+<[-]
]>>+<[>-<-]>[<<<<+>[-]++++++++++>>>-]<<<<<]++++++++++>>[<<->>-]>>>>>>[
-]<<<<<<[-]++++++++++<[->-[>+>+<<-]>>[<<+>>-]<[>+<[-]]>>+<[>-<-]>[>>>+
<<<<<<[-]++++++++++>>>-]<<<<]++++++++++>[<->-]>>>>>>[[>+<-]>++++++++++
++++++++++++++++++++++++++++++++++++++.[-]>+<<]<<<<<<<[>>+>+<<<-]>>>[<
<<+>>>-]<[>+<[-]]>>>>>>>[<<<<<<<+>>+>>>>>-]<<<<<[>>>>>+<<<<<-]<[<+>-]<
[>+<[-]]>[<<<[>>>>>>>>+<<<<<<<<-]>>>>>>>>+++++++++++++++++++++++++++++
+++++++++++++++++++.[-]>[-]+<<<<<<-]<<<<++++++++++++++++++++++++++++++
++++++++++++++++++.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 OKBecause 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.