Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I duplicate a cell value to another cell in Brainfuck?

Tags:

brainfuck

What I want to do is to first ask for an input for the value of cell 0(the first cell), then duplicate that input into the next cell(cell 1) while retaining the input value on cell 0. For example, if I typed in an input of 1, I would expect both cell 0 and cell 1 to have the value of 49(the ascii decimal value of 1 is 49. Check http://www.asciitable.com/ for other ascii values). My idea is to first ask for an input using the , command at cell 0, then duplicating that value to cell 1 and 2 using this code [>+>+<<-](but cell 0 becomes 0 after this loop), then moving the value of cell 2 back to cell 0 using this code >>[<<+>>-](I need the two > at the beginning to move the pointer back to cell 2). I would like to know if there's a faster/more efficient way of doing this. Thanks!

The code im using so far to do this:

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

like image 751
Aiden Chow Avatar asked Sep 18 '25 05:09

Aiden Chow


1 Answers

Nope, there isn't. That's the most efficient way to do it. Brainfuck just isn't a very expressive language.

like image 80
Cedric Mamo Avatar answered Sep 19 '25 22:09

Cedric Mamo