Skip to content

Instantly share code, notes, and snippets.

@maalos
Last active May 15, 2022 11:14
Show Gist options
  • Save maalos/feb0935d8ce4afec0257415ebf8ed186 to your computer and use it in GitHub Desktop.
Save maalos/feb0935d8ce4afec0257415ebf8ed186 to your computer and use it in GitHub Desktop.

brainfuck snippets

arithmetical

addition

|5|3| → |8|0|
   ^       ^
[<+>-]

subtraction

|8|5| → |3|0|
   ^       ^
[-<->]

multiplication

|10|5|0| → |0|5|50|
 ^          ^
[>[->+>+<<]>>[-<<+>>]<<<-]

division (works correctly with divisible numbers, bugs on indivisible)

|8|2|0|0|0| → |0|2|0|0|4|
 ^             ^
[>[->+>+<<]>>[-<<+>>]<[-<<->>]>>+<<<<]

modulo (only works with even numbers, hangs on odd)

|6|0| → |0|3|
 ^       ^
[-->+<]

square number

|5|0|0|0| → |0|0|25|0|
 ^             ^
[->+>+<<]>>[-<<+>>]<<[->[->+>+<<]>>[-<<+>>]<<<]>[-]

cube number

|3|0|0|0|0| → |0|0|0|0|27|
 ^                   ^
[->+>+<<]>[->[->+>+<<]>>[-<<+>>]<<<]>[->[->+>+<<]>>[-<<+>>]<<<]>[-]

operational

clearing current cell

|10| → |0|
 ^      ^
[-]

|–10| → |0|
 ^       ^
[+]

finding next free cell

|*|*|0| → |*|*|0|
               ^
[>]

moving cell value to right

|10|0|0| → |0|10|0|
 ^          ^
[->+<]

moving cell value to left

|0|10|0| → |10|0|0|
   ^           ^
[-<+>]

copying to right

|10|0|0|0| → |10|10|0|0|
 ^            ^
[->+>+<<]>>[-<<+>>]<<

copying to left

|0|0|0|10| → |0|0|10|10|
       ^             ^
[-<+<+>>]<<[->>+<<]>>

string operation

store string until \0

|0|0|0|0|0| → |0|*|*|*|0|
 ^                   ^
>,[>,]

echo string

123 → |1|2|3| → 123
>,[>,]<[<]>[.>]

echo reversed string

123 → |1|2|3| → 321
>,[>,]<[.<]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment