# BrainFuck part 4 - Advanced maths

DPAmar
11.7K views

## Welcome!

playgrounds if you didn't already !

The goal of this playground is to play with maths a bit more. Let's start with something simple : read a number.

In all the previous playground steps, data were read as "chars", mapped to ASCII values, but what if we want to type 123 and have a value 123 (instead of 3 chars)?

## Let's start

• Memory: 0, 0, 0, 0
• Cursor: on first cell
• Input: a decimal number

## Process

• invariant : first cell is the actually read number
• Read next digit on second cell
• While a digit has been read
• Convert char into digit (-48)
• Multiply number of first cell by ten
• Add second cell to first
• More exactly : add first cell 9 times to the second (needs less extra memory)
• then add second to first
• loop
• first cell : the decimal number

## Code

``````>,                      read next digit on second cell
>++++++[-<-------->]  convert into digit
>+++++++++[           do 9 times
<<<[->+>+<<]        add first cell to second and create a save copy
>>[-<<+>>]          put save copy back on first cell
>-]                   loop (9 times)
]<                      loop
``````

## Minified version

``````>,[>++++++[-<-------->]>+++++++++[<<<[->+>+<<]>>[-<<+>>]>-]<<[-<+>],]<
``````

## Final state

• Memory: N, 0, 0, 0
• Cursor: on N
• Input: empty
• Output: unchanged

## Test program

This very basic program reads ASCII code and prints it

``````>,[>++++++[-<-------->]>+++++++++[<  read number
<<[->+>+<<]>>[-<<+>>]>-]<<[-<+>],]<  ** part 2 **
.                                    print as ASCII code
``````

Try with inputs from 65 to 90 for example to have all uppercased letters   