3

I'm trying to make a program in Brainfuck which I think is also called "Brainflake", that will add two single digit decimal integers input with ASCII numeric characters and display the sum in ASCII numeric characters in the output. How would I go about doing so?

asdf3.14159
  • 694
  • 3
  • 19
Jose
  • 31
  • 2

2 Answers2

2

ASCII 0-9 are values 48-57.

So take both your ascii digits, subtract 48 from them and you get a number between 0 and 9.

For B times: subtract 1 from B and add 1 to A

Add 48 back to the result, and you have the ascii value for the sum.

Note that this only works if the sum only has a single digit as well.

Cedric Mamo
  • 1,724
  • 2
  • 18
  • 33
1

As another answer stated, ASCII 0-9 are values 48-57.

++++++++    Set cell 0 to 8
[>++++++<-] Loop: Add 6 to cell1 8 times Cell1 contains 48 cell0 contains 0 ending on cell0
>>          Move to cell2
,           Read ASCII character to cell2
>           Move to cell3
,           Read ASCII character to cell3
<<          Move to cell1
[<+> >-< -] Loop: Add 1 to cell0 and subtract 1 from cell1 and cell2 48 times ending on cell1
<           Move to cell0
[>+< >>>-<<< -] Loop: Add 1 to cell1 and subtract 1 from cell0 and cell 3 48 times ending on cell 0

AT THIS POINT THE CELLS LOOK LIKE THIS (n1 and n2 are the numbers you entered):
0|48|n1|n2

>>>          Move to cell3
[<+> -]      Loop: Add 1 to cell2 n2 times
<<           Move to cell1
[>+< -]      Loop: Add 1 to cell2 48 times
>            Move to cell2
.            Output ASCII character

Here it is without the comments:

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

You can run it here: Try It Online

This service lets you step through the code (add breakpoints with #): https://www.iamcal.com/misc/bf_debug/

asdf3.14159
  • 694
  • 3
  • 19