Jump to content

Brainfuck programming language/Examples

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by 130.94.161.238 (talk) at 04:37, 20 May 2002. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Character I/O
,.
Take one character from the keyboard and output it to the screen.

Simple loop
,[.,]
A continuous loop that takes keyboard input and echoes it to the screen. Note that this assumes 0 to signal the end of input; implementations vary on this point. A version assuming a -1 to signal end of input would be
,+[-.,+]
and one assuming that the current byte stays unchanged on end of input would be
,[.[-],]

Pointer manipulation
,[.>,]
a continuous loop that puts keyboard input into our pointer, outputs it to the screen, then increments our pointer location. This has an advantage over the previous loop, because it is saving our keystrokes into the character array for some theoretical future use. The loop before simply overwrote the same initial byte in the array because it never moved the pointer. Again, this version assumes a 0 to signal end of input.

Additional examples here

Conditional statements
,----------[----------------------.,----------]
This will take lowercase input from the keyboard and make it uppercase. To exit, press press the enter key. Lets look at this program. Pretty long for such a simple task, and it doesn't even do any data validation! the bulk of the visible work it's doing is subtracting 32 from the pointer reference before its output. This is because ASCII uppercase letters are offset -32 from lowercase. But let's look at the whole thing.

First, we input the first character using the (,) and immediately subtract 10. ASCII 10 is the line feed. If the user hit enter, the next command, a loop declaration ([) will not run because we effectively set our pointer ref to zero! Let's examine what happened if the user didn't hit enter, and pressed a valid lowercase letter: So the internal code block is run--first, it decrements the pointer reference by 22. This is because we have already decremented our value by 10. 10 + 22 equals 32. We have changed our value to uppercase.

Next we output it. Now we input the next character, and again subtract 10. If this character was a linefeed, we exit the loop; otherwise, we go back to the start of the loop, subtract another 22, output, and so on.

Addition
,>++++++[<-------->-],,[<+>-],<.>.
This program adds two single-digit numbers and displays the result correctly (assuming it too has only one digit):
$add
4+3
7
$

The first number is input, and 48 is subtracted from it to correct it (the ASCII codes for the digits 0-9 are 48-57). This is done by storing 6 in the second byte, and making a loop that subtracts 8 from the first byte that many times. This is a common method of adding or subtracting large numbers. Next, the plus sign is input in the second byte (which is now zero again); then the second number is input, overwriting the plus sign.

The next loop [<+>-] does the real work, moving the second number onto the first, adding them together and zeroing the second byte. Each time through, it adds one to the first byte and subtracts one from the second; so by the time the second is zeroed, as many have been added to the first byte as have been removed from the second. Now a return is input in the second byte. (We're not error-checking the input at all.)

Then the pointer is moved back to the first byte, which is then output. (The first byte is now a + (b + 48), since we didn't correct b; which is identical to (a + b) + 48, which is what we want.) Now the pointer is moved back to the second byte, which holds the return that was input; it is now output, and we're done.

See also : Brainfuck programming language