esolang

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

Expected: cat

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.

OpcodeNameDoes
00jmpSet the address register to the address
01xorFlip the bit at the address
10inRead an input bit into the address
11outWrite 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.

Expected: Hi

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

Further

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-trance

Source

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