Encoding tricks
The same program, written down a different way. Change the encoding and you change who can read it, how long it is, and what a typo does.
●○○ easy · where to see it
The idea
Many esolangs are an existing language in a new costume. The machine underneath is unchanged; only the way instructions are written changes. Brainfuck alone has hundreds of these. Many are jokes, but they show something real. An encoding is a choice, and it decides what the program looks like to everything that reads it, people, compilers and mutations alike.
Read the story
Common tricks.
- Substitution. Swap each symbol for a word. Ook! (David Morgan-Mar) writes Brainfuck's
>asOok. Ook?and+asOok. Ook.. - Counting. Make the instruction a number of things. In Chicken the number of
chickenwords on a line is the opcode. - Length only. Unary and Lenguage turn a Brainfuck program into one huge number, and the program is that many characters. The content is ignored.
- Invisible. Whitespace uses only space, tab and newline.
- Differences. Piet programs are images; an instruction is the change in hue and lightness from one colour block to the next.
- Compression. Seed stores a length and a random seed. A Mersenne Twister generator expands them into a Befunge-98 program, which is then run.
- Arithmetic. Fractran keeps all its registers in one integer, as exponents of primes. Binary lambda calculus (John Tromp) writes lambda terms as bits;
00is a lambda,01an application,1…10a variable. - Encryption. Malbolge decodes each instruction from its byte and its position in memory, so the same character means different things in different places.
How it works
Unary, step by step (Lode Vandevenne, 2005).
Unary, step by step (Lode Vandevenne, 2005). Each Brainfuck command gets three bits.
> | < | + | - | . | , | [ | ] |
|---|---|---|---|---|---|---|---|
| 000 | 001 | 010 | 011 | 100 | 101 | 110 | 111 |
Take the program ,. (read a character, print it).
- The codes in order are
,= 101 and.= 100. - Put a
1in front, so leading zeros are not lost, giving1 101 100. - Read it as binary, 1101100 = 64 + 32 + 8 + 4 = 108.
- The Unary program is 108 zeros.
To run it, count the zeros, write the count in binary, drop the leading 1 and read off the codes. Every extra command multiplies the length by 8, so Hello World in Unary has more characters than there are atoms in the observable universe. The encoding is complete and utterly impractical.
Lenguage is the same idea with any characters and its own table; only the length counts.
What a typo does. Encodings differ most in their neighbourhoods, meaning which programs are one small change away.
| Encoding of "add 72" | One-character change gives |
|---|---|
Brainfuck: + × 72 | add 71 or add 73 (delete or add one +) |
Decimal: ADD 72 | add 2, 7, 12, 79, 702, … |
| Unary length | a completely different program |
Unary changes value in tiny steps; decimal jumps around; a length-encoding scrambles everything.
Seen in
- Ook!, Chicken. Substitution and counting.
- Unary, Lenguage. A program as one number.
- Whitespace. An encoding you cannot see.
- Piet. Instructions as colour changes.
- Seed. A program as a random seed.
- Fractran, Binary lambda calculus. Programs as arithmetic and bits.
- Malbolge. Position-dependent decoding.
Try this
- Encode the Brainfuck program
+.in Unary. How many zeros? (Answer: 84.) - Decode a Unary program of 1,000 zeros. Is it a valid Brainfuck program?
- Hard: design an encoding of numbers as text where every one-character change moves the value by at most 1. What does it cost in length?