Can someone please post a code piece for multiplying two one-digit numbers in the programming language brainf*ck?
Code for multiplying two one digit numbers in Brainfuck
7.9k views Asked by UgaBuga AtThere are 9 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
Program read to cell #1, #2 and result will be appear in cell #3
I use BF interpreter where I can input numbers as numbers(not ASCII Symbols)
Well, I might not have the most efficient way around it, but it works. I did things in a specific ways so that it would work 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
I know this was posted over eight years ago, but I’d still like to share my answer in case anyone else stumbles across this thread.
,>,>++++++[-<--------<-------->>]<<[->[->+>+<<]>[-<+>]<<]>[-]
>+>[->+<<<<+>>>]>[<<[-]+>>>[-]++++++++++<[->-[>]<<]<[-<<-----
----->>>>>>>+<<<<<]<[-<]>>>]>>>[-<<<<<<+>>>>>>]<<[-]<<<++++++
[-<++++++++<++++++++>>]<.[-]<.[-]
This uses eight cells of space which should all be initialized with zero (in case your using this in a larger program) and the pointer begins at the left most of the eight cells. It will take in two single digit ASCII numbers and output a single two digit ASCII number. By an ASCII number, I mean it will take in and output the ASCII values of the characters making up the number. When this program is done, the pointer will once again be at the left most end of the eight cells and all cells will have been returned to zero. The values this will produce on the tape in normal operation will not go below 0 or exceed 81, so you don’t need to worry about negatives or wrapping.
Post was made 12 years ago but I’d still like to share my answer just incase someone else see this thread.