2

I've just learned about the very basic syntax of Brainfuck and have been trying to quickly challenge myself by programming a "Hello World" script. At the moment, I've got just about every letter up until getting the "o" in hello. As I know, the ASCII code for o is 111. Here is my code to get that number and print the o:

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

Breaking it down, I am setting [2] to 11 by operating 2*5+1. Then, I am adding 10 to [3] every decrement of [2], so 11 times. This should result in 110, so I add one at the end before printing. However, I keep turning up with ASCII code 91, or Hell[. Where did I go wrong?

Thanks in advance!

EDIT: I just changed it to 6 +'s at the beginning and now it works? Even though that would be operating 13*10+1 = 131, not 11? Hmmmmmmm

dsillman2000
  • 976
  • 1
  • 8
  • 20

2 Answers2

1

I know that Brainf*ck does not care about indentation and things. But you should consider to indent the code, because it makes it easier to read.

Let's look at your code:

+++++                  intialize counter (cell #0) to 5
[                      use loop to set cell #2 to 10
    >>++                   add 2 to cell #2
    <<-                
]
[-]                    does actually nothing
>>+                    add 1 to cell #2
[                      use outside with cell #2 as counter
    <<+++++            sets cell #0 to 5
    [                  use inner loop to add 10 to cell #3
        >>>++
        <<<-
    ]
    >>-
]
>+.                    add 1 to cell #3 and print result

Your code is actually working as expected, the reason for not working could be, that the code that is executed before this piece, sets the cells to other initial values.

That's a lot of code to just print a 'o', you could have been printing it after the first loop from cell #2, no need for the second loop. Here's an example for printing "Hello World" with Brainf*ck:

+++++ +++++             initialize counter (cell #0) to 10
[                       use loop to set 70/100/30/10
    > +++++ ++              add  7 to cell #1
    > +++++ +++++           add 10 to cell #2
    > +++                   add  3 to cell #3
    > +                     add  1 to cell #4
<<<< -                  decrement counter (cell #0)
]
> ++ .                  print 'H'
> + .                   print 'e'
+++++ ++ .              print 'l'
.                       print 'l'
+++ .                   print 'o'
> ++ .                  print ' '
<< +++++ +++++ +++++ .  print 'W'
> .                     print 'o'
+++ .                   print 'r'
----- - .               print 'l'
----- --- .             print 'd'
> + .                   print '!'
> .                     print '\n'

The following website could help you by testing your Brainf*ck code: Brainfck Visualizer

GiftZwergrapper
  • 2,602
  • 2
  • 20
  • 40
  • good mentions, though it doesn't directly answer the question. You analyse his code but you don't say what's wrong with it (the code actually isn't wrong but you also don't say that). You say it is a lot of code to print an `'o'`, but that's brainfuck: you need much code to do simple things. You give him an example of a `Hello World!`-program, though he obviously tried to find an own solution. However, some mentions are still good like `does actually nothing` (maybe you could explain *why* `[-]` does nothing in this case, but OK). – Aemyl Nov 16 '16 at 17:57
1

I just tried to run this in my (self-programmed) Brainfuck-interpreter and I got an 'o', like you expected. So maybe you have a problem with your interpreter.

Another possible (and more likely!) reason for your problem is, that your cells have been used before and still have values unequal zero in it. I guess they have been used before because you only show a little part of your code here (You already printed 'hell' (very funny by the way xD) and you skip cell[1]).

I personally split the ASCII-table in blocks of 16 chars. So if I want to get a specific char, I muliply 16 with 3 (for digits), 4 (for uppercase letters) or 6 (for lowercase letters). For some letters though, I muliply 16 with 5 or 7 if it's "faster" (shorter code). The 'o' for example I would get like this:

++++                write 4 in cell(0)
[                   while cell(0) != 0
    >++++           write 4 in cell(1)
    [               while cell(1) != 0
        >+++++++    add 7 to cell(2)
        <-          decrement cell(1)
    ]               go back to 'while cell(1) != 0'
    <-              decrement cell(0)
]                   go back to 'while cell(0) != 0'
>>                  go to cell(2) (has value 4*4*7 = 0x70 or 'p')
-                   decrement cell(2) (it's an 'o' now)
.                   print cell(2)
Aemyl
  • 1,501
  • 1
  • 19
  • 34