Two-digit code in Brainfuck

Maybe someone wants to publish a piece of code to multiply two single-digit numbers in the brainf * ck programming language?

+4
source share
6 answers
,>,< input numbers at cell #1 #2 [ > go to cell #2 [ ->+>+<< move data to cell #3 #4 ] >> go to cell #4 [ -<<+>> move data to cell #2 ] <<< go to cell #1 - decrement cell #1 ] >>. output cell #3 

The program reads cell # 1, # 2 and the result will be displayed in cell # 3

I use a BF interpreter where I can enter numbers as numbers (not ASCII characters)

+8
source

Well, I may not have the most efficient way, but it works. I did something in a certain way so that it works with all of these

2 * 3 = 6

6 * 7 = 42

4 * 5 = 20

So here it is:

 read >, >, << convert from ascii +++++ + [ >----- --- >----- --- <<- ] multiply >[ >[>+>+<<-] >[<+>-] <<- ] separate numbers >[-]>+> >+++++ +++++< [ - >- [>>>]+++++ +++++<<+ [<<<]>>>> ] <- <+++++ +++++>>>[-<<<->>>]<<< convert to ascii <+++++ + [ >+++++ +++> [+++++ +++>] <[<]>- ] print >>[.<<]<[<<]>>. 

I used this interpreter: http://esoteric.sange.fi/brainfuck/impl/interp/i.html

+6
source

well, I was inspired by the first and made it a lot easier:

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

48+ at the end is for bfdev to show it in ascii.

+2
source
 ,>,<[>[>+>+<<-]>>[<<+>>-]<<<-]>>. 
0
source

It seems hard to understand, but it works

 >[>>>+<<<-]>>>[>+>+<<-]>>[<<+>>-]<<<<<<[>+>+>+<<<-]>>>[<<<+>>>-]>>[-<<<[-<<+>>]<[>+>+<<-]>>[<<+>>-]<<>>>>]<[-]<<[-]<[-]< 
0
source

I know this was published over eight years ago, but I would still like to share my answer in case anyone else stumbles upon this topic.

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

This uses eight space cells that must be initialized from scratch (if you use it in a larger program), and the pointer starts to the left of most of the eight cells. It will take two single-valued ASCII numbers and output a single-valued ASCII number. By ASCII number, I mean that it will receive and output the ASCII values ​​of the characters that make up the number. When this program is completed, the pointer will again be at the very left end of the eight cells, and all cells will be returned to zero. The values ​​that will be obtained on the tape during normal operation will not fall below 0 or exceed 81, so you do not need to worry about negatives or hyphenation.

0
source

Source: https://habr.com/ru/post/1341951/


All Articles