You are not logged in.

#1 2011-01-11 14:36:33

kpbotbot
Member
From: Philippines
Registered: 2010-08-02
Posts: 93
Website

Brainf*** programming language. Anyone wanna help me understand?

Okay, so I've come accross this sort of impractical programming language, and I thought it was cool. I took a look at the syntax, a sample code, and looked for an interpreter.

The problem is, I have no idea what in the world I was looking at. I'll post the code and break down a few of my questions. Also, I don't know where to post this (I was thinking of Off-topic. Please move if that categ is more appropriate ^^)

+++++ +++++             initialize counter (cell #0) to 10
[                       use loop to set the next four cells to 70/100/30/10
    > +++++ ++              add  7 to cell #1
    > +++++ +++++           add 10 to cell #2 
    > +++                   add  3 to cell #3
    > +                     add  1 to cell #4
    <<<< -                  decrement counter (cell #0)
]                   
> ++ .                  print 'H'
> + .                   print 'e'
+++++ ++ .              print 'l'
.                       print 'l'
+++ .                   print 'o'
> ++ .                  print ' '
<< +++++ +++++ +++++ .  print 'W'
> .                     print 'o'
+++ .                   print 'r'
----- - .               print 'l'
----- --- .             print 'd'
> + .                   print '!'
> .                     print '\n'

EDIT: sorry. forgot to mention this was from wikipedia.
A few questions smile

1. What's the use of the counters stuff (between increment counter and decrement counter)? How is it related to hello world itself?
2. How is > ++ .  equal to H?



Sorry. I'm only 17 and I've only a few information about programming.  The best that I can do is a Hello World app xD And yes, I tried my best on trying to understand brainf***. Took me hours. Also, I searched the forum.

Last edited by kpbotbot (2011-01-11 15:18:26)


Sex is not the answer.

Sex is the question, and Yes is the answer.

Offline

#2 2011-01-11 14:45:29

Awebb
Member
Registered: 2010-05-06
Posts: 6,662

Re: Brainf*** programming language. Anyone wanna help me understand?

Have you read the wikipedia article? If you don't understand what's written there, maybe you should start with something more practical, like Ook! or LOLCODE.

Online

#3 2011-01-11 14:53:16

theDOC
Member
From: Aachen, Germany
Registered: 2009-06-18
Posts: 50

Re: Brainf*** programming language. Anyone wanna help me understand?

looks a bit like morse code to me

Offline

#4 2011-01-11 15:00:58

kpbotbot
Member
From: Philippines
Registered: 2010-08-02
Posts: 93
Website

Re: Brainf*** programming language. Anyone wanna help me understand?

Awebb wrote:

Have you read the wikipedia article? If you don't understand what's written there, maybe you should start with something more practical, like Ook! or LOLCODE.

Yes. Maybe I'm looking at the wrong places. I'll research more about these pointers thing and cells.

Regarding LOLCODE, it looks easy. I can  understand it a bit since I know some basic programming. And yeah, Ook! is equally confusing as brainf***. Unless I understand these memory pointers thing and cells, I'm not going anywhere xD


Sex is not the answer.

Sex is the question, and Yes is the answer.

Offline

#5 2011-01-11 15:05:18

Ramses de Norre
Member
From: Leuven - Belgium
Registered: 2007-03-27
Posts: 1,289

Re: Brainf*** programming language. Anyone wanna help me understand?

As suggested, read the wikipedia article (from which you probably got this code) and come back with concrete questions about the commands if you don't understand.

As to your second question: it has everything to do with the contents of the byte array that holds the state of the program, the commands "> ++ ." mean "move one entry to the right, increment by two, print the ASCII character with the current byte as value". The command could print any ASCII symbol depending on what's in the byte array.

Offline

#6 2011-01-11 15:05:24

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Brainf*** programming language. Anyone wanna help me understand?

Take a look at the decimal values of the letters:
$ echo Hello World! | od -t d1
72  101  108  108  111   32   87  111  114  108  100   33   10

The program uses the cells for conveniently accessing these numbers:
70 : H (72), W (87)
100 : e (101), l (108), l, o, o, r, l, d
30 : space, !
10 : newline

When you come out of the loop, you are "looking at" cell 0. You move to cell 1 with >. Cell 1 is 70, and the letter H is 72, so you add two with two pluses. And print it with dot.

Offline

#7 2011-01-11 15:36:19

hbekel
Member
Registered: 2008-10-04
Posts: 311

Re: Brainf*** programming language. Anyone wanna help me understand?

The program is "clever" in that it uses four precalculated cells to "get close to" the values of the characters of "Hello World" in advance.

To print the letter H, you could also have used

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ .

e.g. increment cell #0 until it has the value 72, then print it using the ".", and then you'd go on to increment the cell to 101 ("e") with the appropriate number of plusses, and print that, then increment to 108 ("l") and print that twice, etc...

You'd come up with a much more verbose program, but it will result in the same thing.

If you're serious about learning how to program, I'd suggest you take a look at scripting languages like python or ruby. Implementing a brainfuck interpreter in those languages might be a nice learning exercise, though (with the benefit of learning brainfuck in the process).

Offline

#8 2011-01-12 01:35:51

kpbotbot
Member
From: Philippines
Registered: 2010-08-02
Posts: 93
Website

Re: Brainf*** programming language. Anyone wanna help me understand?

Ramses de Norre wrote:

As suggested, read the wikipedia article (from which you probably got this code) and come back with concrete questions about the commands if you don't understand.

As to your second question: it has everything to do with the contents of the byte array that holds the state of the program, the commands "> ++ ." mean "move one entry to the right, increment by two, print the ASCII character with the current byte as value". The command could print any ASCII symbol depending on what's in the byte array.

Yeah, I understand what a 'symbol' does except the brackets. I'll figure that out on my own somehow. What boggled my mind was just the counter part, and how in the world did only 2 + signs equal to 72 xD Thanks smile

Procyon wrote:

When you come out of the loop, you are "looking at" cell 0. You move to cell 1 with >. Cell 1 is 70, and the letter H is 72, so you add two with two pluses. And print it with dot.

Thanks. This was exactly what I was looking for. Now that I know the connection between the two, I may be able to figure out everything on my own now.

hbekel wrote:

The program is "clever" in that it uses four precalculated cells to "get close to" the values of the characters of "Hello World" in advance.

I was about to ask this one. Thanks big_smile


Sex is not the answer.

Sex is the question, and Yes is the answer.

Offline

Board footer

Powered by FluxBB