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 03:03, 30 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. Versions for -1 and "no change" are ,+[-.,+] and ,[.[-],]

Pointer manipulation
,[.>,]
A version of the last one that also saves all the input in the array for future use, by moving the pointer each time.

Conditional statements
,----------[----------------------.,----------]
This will take lowercase input from the keyboard and make it uppercase. To exit, press press the enter key.

First, we input the first character using the (,) and immediately subtract 10 from it. (Unix and Linux use 10 for return. DOS and Windows use 13 followed by 10.) If the user hit enter, the loop command ([) will jump past the end of the loop, because we will have set the first byte to zero. If the character input wasn't a 10, we boldly assume it was a lowercase letter, and enter the loop, wherein we subtract another 22 from it, for a total of 32, which is the difference between an ASCII lowercase letter and the corresponding uppercase letter.

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. When we exit the loop, the program terminates, as there are no more commands.

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

(Now things start to get a bit more complicated. We may as well refer to the bytes in the array as [0], [1], [2], and so on.)

The first number is input in [0], and 48 is subtracted from it to correct it (the ASCII codes for the digits 0-9 are 48-57). This is done by putting a 6 in [1] and using a loop to subtract 8 from [0] that many times. (This is a common method of adding or subtracting large numbers.) Next, the plus sign is input in [1]; 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 [1]. Each time through, it adds one to [0] and subtracts one from [1]; so by the time [1] is zeroed, as many have been added to [0] as have been removed from [1]. Now a return is input in [1]. (We're not error-checking the input at all.)

Then the pointer is moved back to the [0], which is then output. ([0] 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 to [1], which holds the return that was input; it is now output, and we're done.

Multiplication
,>,,>++++++++[<------<------>>-]
<<[>[>+>+<<-]>>[<<+>>-]<<<-]
>>>++++++[<++++++++>-],<.>.

Like the previous, but does multiplication, not addition.

The first number is input in the [0], the asterisk and then the second number are input in [1], and both numbers are corrected by having 48 subtracted.

Now we enter the main multiplication loop. The basic idea is that each time through it we subtract one from [0] and add [1] to the running total kept in [2]. 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 the first number, we could have added one to [4] each time through the outer loop, then moved 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