BrainFuck is like the DNA of any programming language
Brainfuck is an esoteric language with only 8 symbols. But it is Turing Complete : that means that from a theoretical perspective, it is as powerful as any other regular language.
In this article, we are going to show how one can implement basic arithmetic operations in
Brainfuck: addition, multiplication and exponentiation.
The fact that we can do amazing stuff with only 8 characters reminds the fact that our body (and maybe also our mind) is generated with only 4 symbols: A,C,G and T.
In a sense, evaluating
brainfuck code snippets is like exploring the DNA of all computer languages.
If you are new into
brainfuck, you might need to read first this introduction to brainfuck.
Our algorithm for adding two numbers
b is going to be:
+++ store 3 in #0 > move to #1 +++++ store 5 in #1 [-<+>] loop: dec #1 inc #0 < move to #0
The code snippets are live and interactive. So have fun and modify the code ans see its evaluation as you type.
The interactive snippets are powered by a tool of mine named KLIPSE.
For the multiplication of
b, we are going to:
Let’s see how to code
>++++ store 4 in #1 [- loop: dec #1 < move to #0 +++++++ inc 7 times #0 > move to #1 ] < move to #0
Powers of 2
In order to calculate
2^a, our strategy will be:
Initialise the result to 1. Then decrement
aand multiply the result by
Let’s calculate 2 to the 10:
+ store 1 in #0 >>++++++++++ store 10 in #2 [ << loop: [>++<-] #1 contains the double of #0 > move to #1 [<+>-] move the value from #1 to #0 >- dec #3 ] << move to #0
The content of this article has been inspired by Large Numbers in BF.