I saw a TikTok where a dude asked for a cool program that I’ve done in 1’s & 0’s.. but after 15 years of being a programmer, I’ve never done a program in 1’s & 0’s .. ofc he probably meant a program that I’ve compiled. Since the binary attribute to the statement is probably just a saying .. but being a programmer, I am going to take things a bit literal.
The First thing we need is to make a programming language that is written in actual 1’s and 0’s .. the easiest implementation I could think of for an interpreter was the brainfuck language, but since we are using single bits. we only have 2 states. on and off. but brainfuck needs a bit more states. so we are going to write a brainfuck to qubit conversion method. (btw qubit is the name of the language because it deals with superpositions )
So we need to be able to handle all the states of brainfuck, so we need to convert the brainfuck input characters in to 4 bit chunks.. as such the convert is actually easy.
We just need to replace the > with 1111 .. and the < with 0000 and so on. this is done easily by simply loading up the file to a string and doing a string replace and saving the file back down in a qb format.
After that we write a simple qubit interpreter that is a total rip off from a brainfuck interpreter. cuz im lazy as shit..
Then we google some brainfuck examples.. I found the 99 bottles of beer code implemented in brainfuck, so we can easily just convert that to a qubit format with the convert method and then we have a bit ass file with 1’s & 0’s that we can compile/interpret .. so there you go mister tiktok stranger, a program that is done in 1’s & 0’s
using System; | |
using System.IO; | |
using System.Linq; | |
namespace qubit | |
{ | |
public class QuBitInterpreter { | |
public static byte[] stack; | |
public static int pointer; | |
public static void Run(string code) { | |
code = File.ReadAllText(code).Replace("\r", "").Replace("\n", "").Replace("\t", ""); | |
int unmatchedBracketCounter = 0; | |
string[] blocks = Enumerable.Range(0, code.Length / 4).Select(i => code.Substring(i * 4, 4)).ToArray(); | |
for(int i = 0; i < blocks.Length; i++) { | |
switch(blocks[i]) { | |
case "0000": // > | |
pointer++; | |
break; | |
case "1111": // < | |
pointer--; | |
break; | |
case "0110": // + | |
stack[pointer]++; | |
break; | |
case "1001": // - | |
stack[pointer]--; | |
break; | |
case "1100": // . | |
Console.Write(System.Convert.ToChar(stack[pointer])); | |
break; | |
case "0011": // , | |
var key = Console.ReadKey(); | |
stack[pointer] = (byte)key.KeyChar; | |
break; | |
case "1010": // [ | |
if (stack[pointer] == 0) { | |
unmatchedBracketCounter++; | |
while (blocks[i] != "0101" || unmatchedBracketCounter != 0) { | |
i++; | |
if (blocks[i] == "1010") { | |
unmatchedBracketCounter++; | |
} else if (blocks[i] == "0101") { | |
unmatchedBracketCounter--; | |
} | |
} | |
} | |
break; | |
case "0101": // ] | |
if (stack[pointer] != 0) { | |
unmatchedBracketCounter++; | |
while (blocks[i] != "1010" || unmatchedBracketCounter != 0) { | |
i--; | |
if (blocks[i] == "0101") { | |
unmatchedBracketCounter++; | |
} else if (blocks[i] == "1010") { | |
unmatchedBracketCounter--; | |
} | |
} | |
} | |
break; | |
} | |
} | |
} | |
public static void Convert(string bf) | |
{ | |
string code = File.ReadAllText(bf); | |
File.WriteAllText(Path.GetFileNameWithoutExtension(bf) + ".qb", | |
code.Replace(" ", "") | |
.Replace(">", "0000") | |
.Replace("<", "1111") | |
.Replace("+", "0110") | |
.Replace("-", "1001") | |
.Replace(".", "1100") | |
.Replace(",", "0011") | |
.Replace("[", "1010") | |
.Replace("]", "0101") | |
); | |
} | |
public static void Main(string[] args) { | |
stack = new byte[1000]; | |
if(File.Exists(args[0]) && Path.GetExtension(args[0]).ToLower() == ".qb") { | |
Run(args[0]); | |
} else if(File.Exists(args[0])) { | |
Convert(args[0]); | |
} else { | |
Console.WriteLine($"{args} is not a file that i can find.."); | |
} | |
} | |
} | |
} |
000001100110011001100110011001100110011001101010111101100110011001100110011001100110011001100000100101011111100100000000000000000000011001100110101000000110011001100000011001100110111111111001010111111111111111110110111110100000101000000110 | |
000001101111111110010101000000001010100111111111011000000000010101100110011001100000011011111010100111111001000001011111101010101001010100000000100111111111010100000000101010101001010111111111011000000000010111111111101010101001010100000000 | |
000000000000000010101010100101011111011001100110011001100110011001100110011011111001000000000101111110011010000001100000011011111111100101010000101011110110000010010101011000001010101010010101111110010000010111111111111111111111111111111111 | |
111110010000000001011111101000000110000001101111111110010101000000001010100111111111011000000000010101100000011011111010100111111001000001011111101010101001010100000000100111111111010100000000101010101001010111111111011000000000010111111111 | |
111110100000000001100000011011111111111110010101000000000000101010011111111111110110000000000000010101100110000001101111101010011111100100000101111110101010100101010000000010011111111101010000000010101010100101011111111101100000000001011111 | |
111110100000011011111010100101010101111110100000000001101111111110101001010101010000000010101111111101100000000010101001010101011111111111111010000000000110000001101111111111111001010100000000000010101001111111111111011000000000000001010110 | |
011001100110000001101111101010011111100100000101111110101010100101010000000010011111111101010000000010101010100101011111111101100000000001011111111110100000011011111010100101010101111110100000000001101111111110101001010101010000000010101111 | |
111101100000000010101001010101011111111110101010100101010000000000000110011001100110011001100110011010100000000001100110011001100110011011111111100101010000101011110110011001100110011001100110011010100000011001100110011001100110111110010101 | |
000011001111011001100110011001100110011001101010000010011001100110011001100111111001010100001010111111110110000000001001010101010000110011111111011001100110011001100110011001101010000000001001100110011001100110011111111110010101111110101001 | |
000000000110111111110101111101100110011001100110011001100110101011110110011001100110000010010101111111000000011001100110011001100110011010100000011001100110011001100110011001100110111110010101000001100110011011001111011001100110011001101010 | |
000001100110011001100110011001100110011011111001010100001100011001100110011001101100110010011001100110011001100110011001110010011001100110011001100110011100011001100110011001100110011001100110011001100110011001100000000010100000000000000110 | |
000001101111111111111111100101010000000000000000101010011111111111111111011000000000000000000101000001101111101010011111100100000101111110101010100101010000000010011111111101010000000010101010100101011111111101100000000001011111111111111111 | |
101000000000000001100000011011111111111111111001010100000000000000001010100111111111111111110110000000000000000001010110000001101111101010011111100100000101111110101010100101010000000010011111111101010000000010101010100101011111111101100000 | |
000001011111111111111010000000000110111111111010100101010101000010100000011011111010100101010101011001100000000001101111101010011111100100000101111110101010100101010000000010011111111101010000000010101010100101011111111101100000000001011111 | |
011011111010101010010101000010011111010100001010111111111111111111111111111111000000000000000000000000000000101010010101010111111111111111111111111111111111111111000000000010011001100110011100100110011001100110011001100110011001110011111111 | |
110000000000100110011001100111000110011001101100110001100110011001100110011001100110011001100110011001101100101010010101111111111010100101010101111110100000011000000110111111111001010100000000101010011111111101100000000001010110000001101111 | |
101010011111100100000101111110101010100101010000000010011111111101010000000010101010100101011111111101100000000001011111111111111010000000000110000001101111111111111001010100000000000010101001111111111111011000000000000001010110011001100110 | |
000001101111101010011111100100000101111110101010100101010000000010011111111101010000000010101010100101011111111101100000000001011111111110100000011011111010100101010101111110100000000001101111111110101001010101010000000010101111111101100000 | |
000010101001010101011111111110101010100101010000011001100110011001100110011001101010111101100110011001100000100101011111110000000110011001100110011001100110011001100110101000000110011001100110011001100110011001100110011011111001010100000110 | |
110010011100111111111100000000000110011001100110011001101100100110011001100110011001100110011001100110011001110010011001100111001111111111000000011001100110011001100110101000000110011001101111100101010000110011110110011001100110011001101010 | |
000010011001100110011111100101010000011001101100011001100110011001100110011001100110011001101100110010101001010111111111101010010101011001100110011001100110011001100110011011001010100101010101111110100000011000000110111111111001010100000000 | |
101010011111111101100000000001010110011001100000011011111010100111111001000001011111101010101001010100000000100111111111010100000000101010101001010111111111011000000000010111111111101010101001010101100110011001100110011001100110011001101100 | |
000001100110011001100110011001100110011010100000011001100110011001100110011001100110111110010101000001100110011011000110011001100110011001100110011001100110011001100110110001100110011001100110011001100110011001101100100110011001100110011001 | |
110011110110011001100110011001100110011010100000000001100110011001101111111110010101000000001100111101100110011001100110011001100110011001101100100111001001100110011001100110011001100110011100000011001111100111000110011001100110011001100110 | |
011001100110011011000110011001100110011001100110011011001001100110011001100110011001100110011100000011001111100110011001100110011001100110011001100110011001100111000110011001100110011001100110011001100110011001100110110010011001100110011001 | |
100110011001100110011100000011001111011001100110011001100110011001100110011001100110110010011001100110011001100110011001100110011001100110011001100111001111011001100110101000000110011001100110011001101111100101010000110011000000110011111001 | |
100110011001100110011001100110011001110001100110011001100110011001100110011001100110110000001100111111110110011001101010000010011001100110011001100111111001010100001001110001100110011001100110011001100110011001100110011001100110011001100110 | |
110010011001100111000110011001100110011001101100100110011001100110011001100111001001100110011001100110011001100110011001110010101001010100001010100101011111111111111100101010010101010111111010000001100000011011111111100101010000000010101001 | |
111111110110000000000101011001100110011000000110111110101001111110010000010111111010101010010101000000001001111111110101000000001010101010010101111111110110000000000101111111111010101010010101011001100110011001100110011001100110011011001010 | |
100101011111101010010101000001011111011011110101 |
If you like stuff like these, consider leaving a comment or maybe just maybe buying me a coffee. to support to idiotic behavior.. ty..