r/code • • Jul 29 '16

Help Please Please help! Need code to announce our pregnancy to my programmer boyfriend... <3

Hi all, my boyfriend is a Senior Software Engineer... I just found out that we are expecting, and I'd love to break the news to him with a block of code! Trouble is, I don't code... Would you all help me write a small block of code to let him know he's going to be a daddy? TIA!

1.3k Upvotes

209 comments sorted by

View all comments

Show parent comment

161

u/BACON8000 Sep 04 '16

+++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.<++++++++.-------->++++.<.>+++.++.-------------.++.+++++++.-------------.+++++++++++++.++++++.

http://imgur.com/PCUTnFD

42

u/Miodec Sep 04 '16

TIL this is a thing

40

u/BACON8000 Sep 04 '16

It's not as complicated as it sounds to be honest. You just have to interpret it in your brain and it's quite simple, simple enough to write in a Costa Coffee carpark with a crying baby in the back of the car.

So, to begin with, brainfuck uses pointers and values. A pointer is like saying which particular value are we working with, and the value is the actual...you know...value of that particular pointer. > and < move your "current pointer" up and down, or left and right, however you like to see it. + and - increment/decrement the value of that pointer (default is 0). . prints the current value out as ASCII.

So, first I initialise the first pointer to 32, the ASCII for space (for later): +++++++++++++++++++++++++++++++.

Then I move to the next pointer > and increment it right up to 105 (ASCII for "i") and print that out: +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.

After that, I switch back down to the space I prepped earlier, < and increment it up to 39 for a comma, which I print, ++++++++. before returning that back down to 32 for the space --------.

I switch back to my other pointer > and add four to the previous value for m, which I print ++++..

I switch to the other pointer which holds a space and print it, and move back to my other pointer, which is at m: <.>.

I then increment m to p and print +++., then on to r and print ++., then all the way back to e: ------------- and print ..

So now we're at i'm pre, next we need g, ++., and then n, +++++++..

Now we need ant, so, back to a: -------------., forward to n: +++++++++++++. and finally, pop a t on the end: ++++++.

As you can see, if you know the alphabet and the ASCII table (http://www.asciitable.com/index/asciifull.gif) then you can write it on your phone while a baby cries and your girlfriend talks to you (blocked out) without much effort.

5

u/iauu Sep 04 '16

I thought it was much more complicated than that! Maybe it's the fact that it doesn't have any obvious new line or distinct character separating instructions.

Thanks for the explanation!

4

u/phatskat Sep 04 '16

Awesome explanation of brainfuck! Out of curiosity, how would you code a simple if like

if ( X ) { echo "Hi!"; }

1

u/GreyVersusBlue Sep 04 '16

So.... You're writing on two sides of a point, simultaneously, and each time you jump from side to side you're writing a new character? And the character is based on a value determined by the +s and -s?

1

u/bcgoss Sep 04 '16

A pointer is a reference to a memory address. It "points" to that byte and says 'this one'. In brainfuck when you use + or - you change the value of the memory you're pointing to. The period symbol is used to read the memory at the current pointer and interpret it as an ascii character to be printed on the screen.

The < and > symbols are used to switch the pointer you're working with. Your program starts off with pointer 0, and you can use > to switch to pointer 1, then do some stuff and use < to switch back to pointer 0. This is useful because you can make changes to Pointer 0 without affecting pointer 1. You can set one pointer to the ascii value of a symbol and the other to point to the letter m. That way you don't have to step the value of the pointer all the way down to 32 just to use a space.

1

u/GreyVersusBlue Sep 04 '16

Alright. I think I understand it.

So what would be a practical reason to use this, rather than more traditional code?

5

u/bcgoss Sep 04 '16

lol

4

u/bcgoss Sep 04 '16

But realistically, there is no use for brainfuck. It exists to demonstrate what is possible when you have very very simple commands. And to fuck with people.

1

u/werewolf_nr Sep 07 '16

The only real advantage I can think of is that the interpreters can be absurdly small. Less than 1kb for some implementations.

0

u/CoSonfused Sep 04 '16

what kind of thing?

2

u/BACON8000 Sep 04 '16

a bad thing