BrainFuck part 6 - 16-bit integers

DPAmar
1,268 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content
Next: I/O

Welcome!

Welcome to this new playground about the BF language. Make sure you already read

playgrounds if you didn't already !

The goal of this playground is to get rid of a math limition : the "modulo 256" results

What may be annoying using Brainfuck is the size of the cell. You can't (in theory) have integers greater than 255. Of course, this is the case for most of the programming languages, where integers are encoded using either 32 or 64 bits, but the limit is far higher in those cases.

Obviously, the 'cheating' solution would be to use another BF interpreter implementation that supports 16-, 32- or 64-bit integers. But how to use, let's say, 16-bit integers (up to 65,535) using regular Brainfuck? Our only option would be to

  • Define a new data structure
  • Re-define all the 8 operators using this new data structure
    • What does '+' means ? Add 1. So, how to modify our data that represents an integer N to represents integer 'N+1' instead
    • and same for - , . < > [ ] ...

So, how can we have a new data structure to store 16-bit integers (instead of the actual 8-bit) ?

The naive approach would be to use 2 cells per integer. However, we may need some temporary storage in order to perform the overriden operations like + or -.

Let's defined instead a 4-cell-long structure, ABCD, with

  • A = B = 0 (used internally)
  • C and D so that N = D * 256 + C

Using this new structure, let's define other operations.

Note: having cursor on N now means "having cursor on C"

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content