Can someone please post a code piece for multiplying two one-digit numbers in the programming language brainf*ck?
-
1@Gordon: Ironically, if the answer can be found on Wikipedia, the question hardly seems to qualify as "too localized". – Cody Gray - on strike Mar 02 '11 at 10:04
-
@Cody while SO is definitely the right place for "asking how do to do x in programing language y" I dont think an esoteric language like BF qualifies for "being applicable to the worldwide audience of the web" – Gordon Mar 02 '11 at 10:14
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

- 1
- 1

- 81
- 1
- 3
-
-
1How do I enter condition like if my output after multiplication is less than X number or not ? – Rakshit Shah May 25 '19 at 08:03
well, I was inspired by the first one and made it much more simple:
,>,<>[->+>+<<]>>[->>+<<]<[->>>+<<<]>>>++++++++++++++++++++++++++++++++++++++++++++++++
the 48+ in the end is for the bfdev to show it in ascii.

- 48,073
- 15
- 90
- 106

- 21
- 2
Post was made 12 years ago but I’d still like to share my answer just incase someone else see this thread.
,>>+++ +++[<<-------->>-]
<,>+++ +++[<-------->-]
<
[<[>>+>+<<<-]
>>>
[<<<+>>>-]
<<-]
>>++++++[<++++++++>-]
<.

- 11
- 2
-
This will multiply the ASCII values, not the actual values. With this code, '1' * '1' = 49 * 49 = 2401 which, assuming 8 bit cells, would then result in 2401 % 256 = 97. 97 is clearly not equal to 1 * 1. You should subtract 48 (the ascii code of '0') from the character to get the actual value. Like this: ,>++++++[<-------->-],>++++++[<-------->-]<<[>[>+>+<<-]>>[<<+>>-]<<<-]>>. – Cedric Mamo Dec 10 '12 at 09:55
Kinda hard to understand, but it works
>[>>>+<<<-]>>>[>+>+<<-]>>[<<+>>-]<<<<<<[>+>+>+<<<-]>>>[<<<+>>>-]>>[-<<<[-<<+>>]<[>+>+<<-]>>[<<+>>-]<<>>>>]<[-]<<[-]<[-]<

- 27,060
- 21
- 118
- 148

- 41
- 5
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.

- 1
- 1
I know this question is 11 years old but this is for future readers.
,
------------------------------------------------
>,
------------------------------------------------
<
[
>
[
>+>+<<-
]
>>
[
<<+>>-
]
<<<-
]
>>++++++++++++++++++++++++++++++++++++++++++++++++.

- 1,086
- 1
- 8
- 20
I found this VERY VERY simple version that outputs the answer in the second cell
++[>++<-]
This example multiplies 2 by 2 and the number of +s in the beginning and in the bracket loop are the numbers to be multiplied

- 1
- 2