Someone sent this to me and claimed it is a hello world in Brainfuck (and I hope so...)
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
I know the basics that it works by moving a pointer and increment and decrementing stuff...
Yet I still want to know, how does it actually work? How does it print anything on the screen in the first place? How does it encode the text? I do not understand at all...
To answer the question of how it knows what to print, I have added the calculation of ASCII values to the right of the code where the printing happens:
All the answers are thorough, but they lack one tiny detail: Printing. In building your brainfuck translator, you also consider the character
.
, this is actually what a printing statement looks like in brainfuck. So what your brainfuck translator should do is, whenever it encounters a.
character it prints the currently pointed byte.Example:
suppose you have -->
char *ptr = [0] [0] [0] [97] [0]
... if this is a brainfuck statement:>>>.
your pointer should be moved 3 spaces to right landing at:[97]
, so now*ptr = 97
, after doing that your translator encounters a.
, it should then callor any equivalent printing statement to print the currently pointed byte, which has the value 97 and the letter
a
will then be printed on thestd_output
.1. Basics
To understand Brainfuck you must imagine infinite array of cells initialized by
0
each.When brainfuck program starts, it points to any cell.
If you move pointer right
>
you are moving pointer from cell X to cell X+1If you increase cell value
+
you get:If you increase cell value again
+
you get:If you decrease cell value
-
you get:If you move pointer left
<
you are moving pointer from cell X to cell X-12. Input
To read character you use comma
,
. What it does is: Read character from standard input and write its decimal ASCII code to the actual cell.Take a look at ASCII table. For example, decimal code of
!
is33
, whilea
is97
.Well, lets imagine your BF program memory looks like:
Assuming standard input stands for
a
, if you use comma,
operator, what BF does is reada
decimal ASCII code97
to memory:You generally want to think that way, however the truth is a bit more complex. The truth is BF does not read a character but a byte (whatever that byte is). Let me show you example:
In linux
prints:
which is specific polish character. This character is not encoded by ASCII encoding. In this case it's UTF-8 encoding, so it used to take more than one byte in computer memory. We can prove it by making a hexadecimal dump:
which shows:
Zeroes are offset.
82
is first andc5
is second byte representingł
(in order we will read them).|..|
is graphical representation which is not possible in this case.Well, if you pass
ł
as input to your BF program that reads single byte, program memory will look like:Why
197
? Well197
decimal isc5
hexadecimal. Seems familiar ? Of course. It's first byte ofł
!3. Output
To print character you use dot
.
What it does is: Assuming we treat actual cell value like decimal ASCII code, print corresponding character to standard output.Well, lets imagine your BF program memory looks like:
If you use dot (.) operator now, what BF does is print:
Because
a
decimal code in ASCII is97
.So for example BF program like this (97 pluses 2 dots):
Will increase value of the cell it points to up to 97 and print it out 2 times.
4. Loops
In BF loop consists of loop begin
[
and loop end]
. You can think it's like while in C/C++ where the condition is actual cell value.Take a look BF program below:
++
increments actual cell value twice:And
[]
is likewhile(2) {}
, so it's infinite loop.Let's say we don't want this loop to be infinite. We can do for example:
So each time a loop loops it decrements actual cell value. Once actual cell value is
0
loop ends:Let's consider yet another example of finite loop:
This example shows, we haven't to finish loop at cell that loop started on:
However it is good practice to end where we started. Why ? Because if loop ends another cell it started, we can't assume where the cell pointer will be. To be honest, this practice makes brainfuck less brainfuck.
Wikipedia has a commented version of the code.
To answer your questions, the
,
and.
characters are used for I/O. The text is ASCII.The Wikipedia article goes on in some more depth, as well.
I think what you are asking is how does Brainfuck know what to do with all the code. There is a parser written in a higher level language such as Python to interpret what a dot means, or what an addition sign means in the code.
So the parser will read your code line by line, and say ok there is a > symbol so i have to advance memory location, the code is simply, if (contents in that memory location) == >, memlocation =+ memlocation which is written in a higher level language, similarly if (content in memory location) == ".", then print (contents of memory location).
Hope this clears it up. tc
Brainfuck same as its name. It uses only 8 characters
> [ . ] , - +
which makes it the quickest programming language to learn but hardest to implement and understand. ….and makes you finally end up with f*cking your brain.It stores values in array: [72 ][101 ][108 ][111 ]
let, initially pointer pointing to cell 1 of array:
>
move pointer to right by 1<
move pointer to left by 1+
increment the value of cell by 1-
increment the value of element by 1.
print value of current cell.,
take input to current cell.[ ]
loop, +++[ -] counter of 3 counts bcz it have 3 ′+’ before it, and - decrements count variable by 1 value.the values stored in cells are ascii values:
so referring to above array: [72 ][101 ][108 ][108][111 ] if you match the ascii values you’ll find that it is Hello writtern
Congrats! you have learned the syntax of BF
——-Something more ———
let us make our first program i.e Hello World, after which you’re able to write your name in this language.
breaking into pieces:
Makes an array of 4 cells(number of >) and sets a counter of 10 something like : —-psuedo code—-
because counter value is stored in cell 0 and > moves to cell 1 updates its value by+7 > moves to cell 2 increments 10 to its previous value and so on….
<<<
return to cell 0 and decrements its value by 1hence after loop completion we have array : [70,100,30,10]
moves to 1st element and increment its value by 2(two ‘+’) and then prints(‘.’) character with that ascii value. i.e for example in python: chr(70+2) # prints 'H'
moves to 2nd cell increment 1 to its value 100+1 and prints(‘.’) its value i.e chr(101) chr(101) #prints ‘e’ now there is no > or < in next piece so it takes present value of latest element and increment to it only
latest element = 101 therefore, 101+7 and prints it twice(as there are two‘..’) chr(108) #prints l twice can be used as
———Where is it used?——-
It is just a joke language made to challenge programmers and is not used practically anywhere.