# LebGeeks

A community for technology geeks in Lebanon.

You are not logged in.

## #1 November 1 2011

Joe
Member

### [Exercise] Brainfuck exercise.

Brainfuck, despite its name, is actually a pretty interesting programming language. Sure it's esoteric. Sure you won't build a whole CRUD app in it. Sure you won't probably ever do anything useful in it. However there are some cool mental exercises you could do.

Brainfuck is all about manipulating simple numbers. The permitted operations are so simplistic, things as simple as multiplications took me some time to understand.

Here's how you multiply 3 by 5:

+++
[
>
+++++
<
-
]

Or more simply:

+++[>+++++<-]

The code itself is pretty self-explanatory, however I should warn you I had to read it a few times before I could understand it.

Today's exercise is simple, and based on the same principle as above:

1- Move a number from byte 1 to byte 2.
2- Copy a number from byte 1 to byte 2.

The second one may be a bit trickier, but once you actually find the solution, the feeling of accomplishment is so cool, you won't believe what you just did :)

I strongly recommend this exercise to people who enjoyed math riddles and quizzes at school. No prior knowledge of programming is really necessary. Just take your time.

Extra ressources

If you're unfamiliar with Brainfuck, you can check the rules in our previous exercise. A good way to start is to actually write your own interpreter.

If you don't want to write your interpreter, try to download one, there are thousands available. You can use mine. It's not perfect but it works.

3rd Question
If you successfully manage to do the first two questions, than this one should be easy for you:

3- Write an program that asks the user for two numbers and then multiply them

## #2 November 1 2011

geek
Member

### Re: [Exercise] Brainfuck exercise.

1 to 2
,[>+<-]>.

1 to 2,3
3 to 1
,[>+>+<<-]>>[<<+>>-]<<.>.

## #3 November 1 2011

Joe
Member

### Re: [Exercise] Brainfuck exercise.

geek you rock!

I did the second one a bit differently:

1 to 3
3 to 2,1
,[>>+<<-]>>[<+<+>>-]<<.>.

It's just a detail :)

Since you answered so fast, I'm adding a 3rd question that is the real exercise.

## #4 November 2 2011

geek
Member

### Re: [Exercise] Brainfuck exercise.

while (#1--) { while (#2--) { #3++; #4++ } #2 = #4 } }

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

Last edited by geek (November 2 2011)

## #5 November 2 2011

BashLogic
Member

### Re: [Exercise] Brainfuck exercise.

I can only say one thing... Geeks ;)

## #6 November 2 2011

Joe
Member

### Re: [Exercise] Brainfuck exercise.

geek you're missing the conversion from ASCII to actual numbers. It's usually (if n<10) a translation by 48. Something like this:

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

I'll check the rest at home tonight, but it looks great :)

## #7 November 2 2011

arithma
Member

### Re: [Exercise] Brainfuck exercise.

It's fun checking these out at:
http://www.iamcal.com/misc/bf_debug/
Nice work geek.

## #8 November 19 2011

Joe
Member

### Re: [Exercise] Brainfuck exercise.

>,                      # reads first input in p1
>++++++[<-------->-]    # convert from ASCII to number
,                       # reads first input in p2
>++++++[<-------->-]    # convert from ASCII to number

<<
[                       # while val(p1) not 0
>[>>+<<-]>>[<+<+>>-]<  # cp val(p2) to p3
[<<<+>>>-]             # mv val(p3) to p0
<<-                    # decreases val(p1)
]

++++++[<++++++++>-]<.  # translates back to ASCII and print

## #9 April 1 2014

NuclearVision
Member

### Re: [Exercise] Brainfuck exercise.

1-

,[>+<]>

this also outputs the number.
2-

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

did this by temporary lending the 3rd byte
Any feedback?

Last edited by NuclearVision (April 11 2016)

## #10 April 1 2014

Johnaudi
Member

### Re: [Exercise] Brainfuck exercise.

NuclearVision wrote:

1-

,[>+<.]>.

this also outputs the number.
2-

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

did this by temporary lending the 3rd byte
Any feedback?

Why copy and paste the inputted var to the second and third byte? (when you're doing ,[>+>+<<-])

## #11 April 2 2014

NuclearVision
Member

### Re: [Exercise] Brainfuck exercise.

Johnaudi wrote:
NuclearVision wrote:

1-

,[>+<.]>.

this also outputs the number.
2-

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

did this by temporary lending the 3rd byte
Any feedback?

Why copy and paste the inputted var to the second and third byte? (when you're doing ,[>+>+<<-])

Because if I don't use one of the cells(vars) as a loop index I will get an infinite loop, if I use only the first cell to loop then I'd be moving the input from 1st to 2nd bytes, not copying it. By using the third byte I killed and ended the loop in third byte and got the input copied to 2 other bytes.

## #12 April 2 2014

Johnaudi
Member

### Re: [Exercise] Brainfuck exercise.

NuclearVision wrote:
Johnaudi wrote:
NuclearVision wrote:

1-

,[>+<.]>.

this also outputs the number.
2-

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

did this by temporary lending the 3rd byte
Any feedback?

Why copy and paste the inputted var to the second and third byte? (when you're doing ,[>+>+<<-])

Because if I don't use one of the cells(vars) as a loop index I will get an infinite loop, if I use only the first cell to loop then I'd be moving the input from 1st to 2nd bytes, not copying it. By using the third byte I killed and ended the loop in third byte and got the input copied to 2 other bytes.

But the second byte has completely no use.

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

Can be translated to:

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

## #13 April 2 2014

NuclearVision
Member

### Re: [Exercise] Brainfuck exercise.

@johnaudi your code moves data from byte 1 to byte 2 then from byte 2 to byte 1. In other terms your code isn't copying but rather moving then deleting.

rahmu wrote:

2-copy a number from byte 1 to byte 2

The thing behind this exercise is to start from a number @byte1, at the end you should have the same number @byte1,2.

## #14 April 2 2014

Johnaudi
Member

### Re: [Exercise] Brainfuck exercise.

NuclearVision wrote:

@johnaudi your code moves data from byte 1 to byte 2 then from byte 2 to byte 1. In other terms your code isn't copying but rather moving then deleting.

rahmu wrote:

2-copy a number from byte 1 to byte 2

The thing behind this exercise is to start from a number @byte1, at the end you should have the same number @byte1,2.

Ah, my apologies, I missed the point of the exercise and focused more on your code.

## #15 April 4 2016

barrad
Member

### Re: [Exercise] Brainfuck exercise.

What if all of the 30000 bytes are occupied  ?

## #16 April 5 2016

Joe
Member

### Re: [Exercise] Brainfuck exercise.

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

I think there's a mistake in this one. The 2nd "," is doing something wrong.

What if all of the 30000 bytes are occupied  ?

Well according to the language definition 30,000 is all you get. I guess you'll need to be smart to overcome this limitation.

Now I'm pretty sure you're allowed to add more bytes in your implementation if needed, no one will mind :)