Thursday, November 5, 2009

My experience with BrainF_ck Programming Language

Hi folks.. Last few days I worked a bit on BrainF_ck language. A nice language that may be used for encoding sort of stuff. What we code in it.. it might happen that we ourselves forget about it, such is the level of complexity of the language!!
The brilliant factor about the language is that it is just made of eight symbols in total..!! Surprised.. so was I..!! :D

Whole of the language depends on just an array. Every manipulation has to be done in the array using a memory pointer, which moves through the memory blocks of the array. Now, getting over to the practical implementation of the language. The eight symbols are:

> = increases memory pointer, or moves the pointer to the right 1 block.
< = decreases memory pointer, or moves the pointer to the left 1 block.
+ = increases value stored at the block pointed to by the memory pointer
- = decreases value stored at the block pointed to by the memory pointer
[ = like c while(currentBlockValue != 0) loop.
] = if block currently pointed to's value is not zero, jump back to [
, = like c getchar(). input 1 character.
. = like c putchar(). print 1 character to the console.
I made my first program for SPOJ challenge problem BFWRITE. Before going through the code I would give you the link of an online compiler for BrainF_ck language. I prefer BrainF interpreter. So now, here's my code:
  1. ++++++++++
  2. [
  3. >+++++++>++++++++++>+++<<<-
  4. ]
  5. >+++++++++++++.
  6. ---.
  7. -.
  8. -----.
  9. >>++.
  10. <+++++.
  11. ++++++++++.
  12. >.
  13. <----------.
  14. +++++.
  15. ----------.
  16. +.
  17. .
  18. -.
  19. >.
  20. <---.
  21. ++++++++++++++++++++++.
  22. ------------------.
  23. ++++++++++++++.
  24. ----.
  25. --.
  26. --------.

Looks quite difficult to understand but I'll make it simpler for you.
  • First line assigns value 10 to a[0]
  • Second line begins a loop checking value of current block a[0].
  • Third line moves pointer ahead once(>) and assigns value 7 to a[1](++++++). The same line then again pushes pointer to a[2] and assigns value 10. 3 value is given to a[3]. The pointer returns back to a[0](<<<) and decrements the value to 9.
  • In the fourth line.. loop goes on until the value of a[0] exhausts.
  • Finally the values are.. a[0]=0, a[1]=70, a[2]=100, a[3]=30.
  • The sixth line decreases a[1] by 3 and makes its value equal to 67. '.' prints the value 'S', ASCII code of 67.
  • Likewise the further lines print the text:- 'P','O','J',' ','i','s',' ','i','n','d','e','e','d',' ','a','w','e','s','o','m','e'. Hence on our output comes as:- SPOJ is indeed awesome.
I further improved my code to 153 characters. Instead of making the array 70, 100, 30 in the beginning I made the array as 64, 32, 104, 112. Try it out yourself to optimize the code.

* * * * *


  1. I think, C is widely used for developing portable application software. C is one of the most popular programming languages. It is widely used on many different software platforms, and there are few computer architectures for which a C compiler does not exist. C has greatly influenced many other popular programming languages, most notably C++, which originally began as an extension to C.


  2. yaa.. c/c++ is also extensively used in embedded systems, manufacturing drivers, manufacturing compilers, OS, socket programming and even in game designing..!! ;)