Brainfuck programming language/Examples
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.
Multiplication
,>,,>++++++[<--------<-------->>-]
<<[>[>+>+<<-]>>[<<+>>-]<<<-]
>>>++++++[<++++++++>-],<.>.
This program multiplies two single-digit numbers and displays the result correctly (assuming it too has only one digit):
$mult
2*4
8
$
The first number is input in the first byte, the asterisk and then the second number are input in the second byte, and both numbers are corrected by having 48 subtracted.
Now things get a bit more complicated. We may as well refer to the bytes in the array as [0], [1], [2], and so on. The two corrected numbers are now in [0] and [1].
Now we enter the main multiplication loop. The basic idea is that each time through it we subtract one from [0], while adding [1] to the running total kept in [2] and keeping other things the same. In particular: the first inner loop moves [1] onto both [2] and [3], while zeroing [1]. (This is the basic way to duplicate a number.) The next inner loop moves [3] back into [1], zeroing [3]. Then one is subtracted from [0], and the outer loop is ended. On exiting this loop, [0] is zero, [1] still has the second number in it, and [2] has the product of the two numbers. (Had we cared about keeping [0], we could have added one to [4] each time through the outer loop, then copied the value from [4] back to [0] afterward.)
Now we add 48 to the product, input a return in [3], output the ASCIIfied product, and then output the return we just stored.
- See also : Brainfuck programming language