brainfuck 一览

This is My Experience of Using brainfuck for 30 Minutes

A Quick Introduction

According to wikipedia:

“Brainfuck is an esoteric programming language noted for its extreme minimalism. The language consists of only eight simple commands and an instruction pointer. It is designed to challenge and amuse programmers, and was not made to be suitable for practical use. It was created in 1993 by Urban Müller.”

 

3 Reasons to Learn brainfuck
  • You’re bored
  • You want to show off to your mates
  • You want hot girls to throw themselves at you
Commands in brainfuck

There is a total of 8 commands in brainfuck. Each command consists of a single character and every character that is not a command is ignored by the interpreter.

Brainfuck is represented by an **array with 30,000 cells** initialized to zero and a data pointer pointing at the current cell (Starting at cell 0).

The eight commands are:

CommandExplanation
+Increments the value at the current cell by one.
-Decrements the value at the current cell by one.
>Moves the data pointer to the next cell (cell on the right).
<Moves the data pointer to the previous cell (cell on the left).
.Prints the ASCII value at the current cell (i.e. 65 = ‘A’).
,Reads a single input character into the current cell.
[If the value at the current cell is zero, skips to the corresponding ] . Otherwise, move to the next instruction.
]If the value at the current cell is zero, move to the next instruction. Otherwise, move backwards in the instructions to the corresponding [

My First Ever brainfuck Program

This took me 15 to 20 minutes to create, which includes the time I spent learning the language. To execute it, I am using an online brainfuck interpreter, which is written in javascript and it works pretty well. Check it out HERE My first program is pretty simple, it just prints the word “ROBBIE” to the screen. Here it is:

Explanation

To explain what’s going on, I’ll break the program down and explain every single command to help you understand what exactly is going on. Also, remember that any character that is not a command is ignored by the interpreter i.e. every alpha-numeric character and white space.

Tip: If you have a pencil and paper, it might be useful to draw an array to keep track of what’s in each cell.

How the original code prints each letter:

Printing the letter R

We want to print R, that means we’ve got to set a cell in the array to 82 (the ASCII number for R). Sounds easy, right? You could simply insert 82 + commands and output it using the . command .

It’s correct, but there’s a cleaner and a more clever way to do it using loops.

We are going to use cell 0 as the loop counter. When the loop counter gets to 0, the loop will end and we’ll move on to the next instruction after the ] command.

I’ll give you the algorithm for printing the letter R using loops:

Initialise cell 0 to 8
While cell 0 != 0
move to the next cell (cell 1)
increment cell 1 by 10
move back to the previous cell (cell 0)
decrement cell 0 by 1

Code to Print R

Command by command instructions:

Printing the Letters OBB

This is very similar to Printing R. If you understand that, then you should have no problem understanding how to print OBB.

Printing I

This is a different approach to printing a letter. Here, we will use the value of the previously assigned cell (the letter B in this case).

We are going to move the value from cell 3 (66 i.e. the letter B ) into cell 4, and then manipulate the value of cell 4 to make it equal to 73 (i.e. the letter I).

To do this we need to move 66 (B) to the next cell and add 7 to turn it into an I.

The code to move the current cell into the next cell:

Code explained command by command:

Printing the Letter E

You’ve just printed the letter I. Printing E uses the same concept as printing I (moving the value (I) to the next cell and manipulating it to change it to an E)

Finished


Basic Algorithms

Now that you’ve conquered the basics of looping, here is a list of useful brainfuck algorithms.

There you have it. You’ve created your first brainfuck program, learned about loops and moving a value from one cell to the next, and you’ve got a list of the basic algorithms that you can play around with.


原文地址:http://robl.co/brief-look-at-brainfuck/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值