Skip to content

Instantly share code, notes, and snippets.

@SansWord
Last active March 29, 2021 23:20
Show Gist options
  • Save SansWord/518a4eb8530762110fac71a392c07c4a to your computer and use it in GitHub Desktop.
Save SansWord/518a4eb8530762110fac71a392c07c4a to your computer and use it in GitHub Desktop.
Brainfuck Hello World! explained
start with an array of unlimited size all intialized to 0and the curser point to 0.
with a input stream and output stream.
>: move cursor to next position
<: move cursor to last position
+: add 1 to current position
-: minus 1 to current position
[: if current position is 0, jump to paired ], otherwise execute next line
]: if current position is non-0, scroll back to paired [, otherwise execute next line
.: output current position to the output stream (usually ASCII encoding)
,: read a byte from the input stream and save to current position
Hello World! in one line:
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
Tracing:
|data(zero-based), ACTION, (cursor position)
+++++ +++ |8(0)
[ |
>++++ |8, 4 (1)
[ |
>++ |8, 4, 2 (2)
>+++ |8, 4, 2, 3 (3)
>+++ |8, 4, 2, 3, 3 (4)
>+ |8, 4, 2, 3, 3, 1 (5)
<<<<-|8, 3, 2, 3, 3, 1 (1)
] |
|8, 0, 8, 12, 12, 4 (1) (fast-forward)
>+ |8, 0, 9, 12, 12, 4 (2)
>+ |8, 0, 9, 13, 12, 4 (3)
>- |8, 0, 9, 13, 11, 4 (4)
>>+ |8, 0, 9, 13, 11, 4, 1 (6)
[ |
< |8, 0, 9, 13, 11, 4, 1 (1)
] |
<- |7, 0, 9, 13, 11, 4, 1 (0)
] |
|0, 0, 72, 104, 88, 32, 8 (0) (fast-forward)
>>. |0, 0, 72, 104, 88, 32, 8 PRINT(72) (2)
>---. |0, 0, 72, 101, 88, 32, 8 PRINT(101) (3)
+++++ ++.. |0, 0, 72, 108, 88, 32, 8 PRINT(108), PRINT(108) (3)
+++. |0, 0, 72, 111, 88, 32, 8 PRINT(111) (3)
>>. |0, 0, 72, 111, 88, 32, 8 PRINT(32) (5)
<-. |0, 0, 72, 111, 87, 32, 8 PRINT(87) (4)
<. |0, 0, 72, 111, 87, 32, 8 PRINT(111) (3)
+++. |0, 0, 72, 114, 87, 32, 8 PRINT(114) (3)
----- -. |0, 0, 72, 108, 87, 32, 8 PRINT(108) (3)
----- ---. |0, 0, 72, 100, 87, 32, 8 PRINT(100) (3)
>>+. |0, 0, 72, 100, 87, 33, 8 PRINT(33) (5)
>++. |0, 0, 72, 100, 87, 33, 10 PRINT(10) (6)
--------------
output: 72 101 108 108 111 32 87 111 114 108 100 33 10
Hello World! in ASCII
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment