BitShift

From Esolang
Jump to navigation Jump to search

BitShift was created by user:Bas to reminisce the old days, when computers only understood zeroes and ones.

Language overview

A BitShift program only knows of 2 valid instructions, 0 and 1. Other characters are invalid and will thus throw an error on execution. Instructions are interpreted as alternating 0's and 1's. Whenever 2 of the same instructions are entered, the program will count the alternating instructions preceding it and perform a command. It doesn't matter if you start your program with a 0 or a 1.

0101011010011101011 will perform the instructions 6, 4, 2, 1, 5, 1

010101 1010 01 1 10101 1. Here, the non alternating characters have been seperated by a space for improved readability.

BitShift programs don't have any stack operations or loops. They have one single value which can be altered.
This value can range from 0-255 (0000 0000 - 1111 1111)

Instructions

BitShift supports the following instructions:

Alternations Description
1 Shift the value 1 bit to the left (0000 0001 > 0000 0010)
2 Shift the value 1 bit to the right (0000 0010 > 0000 0001)
3 XOR the value with 1 (0000 0000 > 0000 0001)
4 XOR the value with 128 (0000 0000 > 1000 0000)
5 Set the value to 0
6 Convert the value to an ASCII character and print it
7 Read an ASCII character from user input and set the value to it

Examples

Hello, World!

0100000100000101011010011011010100110100110100110101001010110010001001010111100110011110101001000000010101101111011110111011101101010001000101100101011111011101001010110111101110111101010001000010101111101101010

Cat

0101010010101

Implementations

The simplistic nature of BitShift is reflected in the following implementation in Common Lisp:

 (defun interpret-BitShift (code)
   "Interprets the BitShift CODE and returns ``NIL."
   (declare (type string code))
   (let ((value        0)
         (alternations 0))
     (declare (type (unsigned-byte 8) value) (type (integer 0 *) alternations))
     (flet ((execute-command ()
             (case alternations
               (1 (setf value (ldb (byte 8 0) (ash value  1))))
               (2 (setf value (ldb (byte 8 0) (ash value -1))))
               (3 (setf value (logxor value 1)))
               (4 (setf value (logxor value 128)))
               (5 (setf value 0))
               (6 (write-char (code-char value)))
               (7 (format T "~&Please input a character: ")
                  (setf value (char-code (read-char)))
                  (clear-input))
               (T (error "Invalid alternations: ~d." alternations)))))
       (loop
         for previous-bit of-type (or null character) =      NIL then current-bit
         for current-bit  of-type (or null character) across code
         for position     of-type fixnum              from   0
         do  (cond
               ((not (member current-bit '(#\0 #\1) :test #'char=))
                 (error "Not a bit at position ~d: ~s." position current-bit))
               ((null previous-bit)
                 (incf alternations))
               ((char/= previous-bit current-bit)
                 (incf alternations))
               (T
                 (execute-command)
                 (setf alternations 1)))
         finally (execute-command)))))

Interpreter