Friday, 17 November 2017

Brainfuck: Sum of two numbers

Read two numbers from user and print the sum of those numbers.

How to read input from user?
‘,’ command is used to read the input from user.

How to calculate the sum of two numbers?
First of all when you read the input from user, it is stored in the cells as ASCII number.

Decimal Number
ASCII Equivalent
0
48
1
49
2
50
3
51
4
52
5
53
6
54
7
55
8
56
9
57

By subtracting the ASCII equivalent of a number with 48 we can get the actual number.
0 = 48 – 48
1 = 49 – 48
2 = 50 – 48
3 = 51 – 48
4 = 52 – 48
5 = 53 - 48
6 = 54 - 48
7 = 55 - 48
8 = 56 – 48
9 = 57 – 48

After reading the two numbers, we should make them to their decimal equivalents.

I already explained how to do that in my previous post.

,                # Read first number    
>                # Move the data pointer to next cell
,                # Read the second number
>                # Move the data pointer to third cell 

# Logic to convert ASCII numbers to their decimal format
++++++++         # Init third cell to 8 
[
 -
 <
 ------
 <
 ------
 >>
]

# Sum the numbers
<                  # Move the pointer from 3rd cell to 2nd cell
[
  -
  <
  +
  >
]

# Move to 1st cell and print the sum
< .




Previous                                                 Next                                                 Home

Brainfuck: Convert ASCII to decimal number

First of all when you read the input from user, it will be stored as ASCII equivalent.

Decimal Number
ASCII Equivalent
0
48
1
49
2
50
3
51
4
52
5
53
6
54
7
55
8
56
9
57

By subtracting the ASCII equivalent of a number with 48 we can get the actual number.
0 = 48 – 48
1 = 49 – 48
2 = 50 – 48
3 = 51 – 48
4 = 52 – 48
5 = 53 - 48
6 = 54 - 48
7 = 55 - 48
8 = 56 – 48
9 = 57 – 48

Below snippet reads a number and convert it to decimal.

,                #Read a number

# Logic to convert ASCII numbers to their decimal format
>
++++++++         # Init second cell to 8 
[
 -
 <
 ------
 >
]
<


Previous                                                 Next                                                 Home

Brainfuck: print ASCII Table

ASCII table has 256 (from 0 to 255) characters. To print all the characters of ASCII table, I would like to go with below approach.

Step 1: File the 1st cell with value 255. Below snippet do that.
+++++++++++++++++  # Initialize the 0th to 17
>
++++++++++++++  # Initialize the 1st cell to 15
[
 -
 <
 +++++++++++++++++   # Increment the first cell by 17
 >
]

Step 2: Print the 1st cell content and decrement the value, repeat step2, until it reaches to 0. Below snippet do that.

[.-]

Find the below working application

+++++++++++++++++  # Initialize the 0th to 17
>
++++++++++++++  # Initialize the 1st cell to 15
[
 -
 <
 +++++++++++++++++   # Increment the first cell by 17
 >
]

<      Move the data pointer to 0th cell

[.-]

When you ran above application, it prints the ASCII table from bottom(255) to top (0).




Previous                                                 Next                                                 Home

Brainfuck: Read and print infinite data

Command ‘,[.>,]’ is used to read infinite data and print to console.

I am using below online visualizer.

Previous                                                 Next                                                 Home

Brainfuck: Read value

‘,’ command is used to read a value from user.

Below statement read the input from user and print to console.

,.

I am using below online visualizer.
Type the input and press Enter, you can able to see the output at bottom of the window.

abc

Previous                                                 Next                                                 Home

Brainfuck: Hello World application

It is time to write first application in Brainfuck.

Problem: Print numbers from 0 to 9.

Approach 1
Let me start with naïve approach.

By default, all the array cells are initialized with 0.
0
0
0
0
0
0
0
0
0
0

Final output should be like below.
0
1
2
3
4
5
6
7
8
9

As per ASCII table, decimal number 48 represents 0, 49 represent 1 etc.,

Number
ASCII equivalent
0
48
1
49
2
50
3
51
4
52
5
53
6
54
7
55
8
56
9
57

To get the output, we should fill the 0th cell with 48, 1st cell with 49, 2nd cell with 50 etc.,

‘+’ command is used to increment the value of the cell by 1.
‘.’ Is used to print the value at the current cell.
‘>’ command is used to move the data pointer to once cell right.

48 +’s make the cell value to 48. After that we can print the value and move to next cell. Do 49 +’s to make the cell value to 49, next print the value and move to next cell, repeat this procedure for next 8 times.


Find the below working application.

++++++++++++++++++++++++++++++++++++++++++++++++
.
>
+++++++++++++++++++++++++++++++++++++++++++++++++
.
>
++++++++++++++++++++++++++++++++++++++++++++++++++
.
>
+++++++++++++++++++++++++++++++++++++++++++++++++++
.
>
++++++++++++++++++++++++++++++++++++++++++++++++++++
.
>
+++++++++++++++++++++++++++++++++++++++++++++++++++++
.
>
++++++++++++++++++++++++++++++++++++++++++++++++++++++
.
>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
.
>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
.
>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
.

Output
0123456789

Approach2
To print the values from 0 to 9, we should fill the cells like below.

48
49
50
51
52
53
54
55
56
57

We can perform the operation using looping.


Step 1: First let us initialize the counter (first cell) to 10.

‘++++++++++’ initialize first cell to 10.

Step 2: By using the loop [], fill all the cell values to 50.

[
  > +++++                   Fill each cell with 5
  > +++++
  > +++++
  > +++++
  > +++++
  > +++++
  > +++++
  > +++++
  > +++++
  > +++++

  <<<<<<<<<< -               Move the data pointer to 0th cell
]

After this step, array looks like below.
50
50
50
50
50
50
50
50
50
50

Step 3: We need to convert above table to print 0 to 9. First cell has value 50, we should decrement by 2 to make it 48.

Apply the same procedure for all other cells.


Find the below working application.

++++++++++                   initialize counter (cell #0) to 10
[
  > +++++                   Fill each cell with 5
  > +++++
  > +++++
  > +++++
  > +++++
  > +++++
  > +++++
  > +++++
  > +++++
  > +++++

  <<<<<<<<<< -               Move the data pointer to 0th cell
]

All the cells has value 50

>  --
.

> -
.

>
.

> +
.

> ++
.

> +++
.

> ++++
.

> +++++
.

> ++++++
.

> +++++++
.


Output

0123456789



Previous                                                 Next                                                 Home

Setting up Visual brainfuck

Download visual brainfuck from below location.

Run the setup file, and follow the instructions. Once the installation is successful, open ‘Visual brainfuck’ IDE. You can able to see below kind of window.


Previous                                                 Next                                                 Home