Bitwise Trance
Every string of bits is a program. It reads its own instructions out of the memory it changes.
Hakerh400, 2019 · Turing complete: unknown · ●●● hard · esolangs.org
Tested with 2 known programs · Test method
Run it
The idea
Hakerh400 made Bitwise Trance in 2019. The specification calls it close to an assembler with unbounded memory. The program is a string of bits at the start of a memory that is 0 everywhere else, and one address register reads it from address 0. There is no halt and no alignment. A jump can land inside another instruction, and the program can flip the bits of the code it is about to read.
Read the story
The specification thinks it is probably Turing complete, but that has not been proven.
The machine
An instruction is addr0 op1 addr1 op2 addr2. If the bit at addr0 is 0, op1 runs on addr1; otherwise op2 runs on addr2.
| Opcode | Name | Does |
|---|---|---|
00 | jmp | Set the address register to the address |
01 | xor | Flip the bit at the address |
10 | in | Read an input bit into the address |
11 | out | Write the bit at the address |
After anything but a jump the register moves past the instruction. An address is read in pairs of bits. The first bit of a pair is 1 to go on, and a single 0 stops. The second bits, lowest first, give a number, and k of them add 2^k minus 1. The example 101110100 holds the bits 0, 1, 0, 0, which is 2, and with k = 4 that gives 17.
Output also comes in pairs. A 1 and then a bit writes that bit, and a 0 where a pair starts ends the program; that is the only way it stops. The bits make bytes, low bit first. Input is given in the same pairs, a 1 before each bit and a 0 at the end, which is what the cat needs.
Example
The cat reads a bit into address 0, writes it out and jumps back to 0.
The cat reads a bit into address 0, writes it out and jumps back to 0. It copies the pairs of its input, the final 0 too, so it stops when the input does.
Text is a row of two instructions per bit. 011100000 is 0 out 1 jmp 0 and writes the 1 found at address 1. 0110000 is 0 out 0 jmp 0 and writes the 0 at address 0. The Hello World program is 1,763 bits built this way; Hi takes 271.
The truth machine works on bits. On a 0 it writes the bit 0 and stops, and on a 1 it writes 1s for ever. One bit makes no whole byte, so it prints nothing on 0 and the byte 255 again and again on 1.
Patterns it teaches
- Every text runs: any string of bits decodes to instructions.
- Self-modifying code: code and data share one memory of bits.
- Encoding tricks: addresses of any size from pairs of bits, and a halt made out of the output.
Further
See also
String-rewriting paradigm
Write your own in Run Convert a program from Bitwise Trance
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: bitwise-trance.
Command line
shib run bitwise-trance -e '01001000110110000001' --input 'cat'shib manual bitwise-tranceSource
- Module
langs/wiki/ports.js- Interpreter
langs/wiki/bitwise-trance.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org