# Programming with Bits and Bytes

In this challenge you are going to write an interpreter for a simple language I've made up. The language is based on a single accumulator A, which is exactly one byte in length. At the start of a program, A = 0. These are the languages instructions:

## !: Inversion

This instruction simply inverts every bit of the accumulator. Every zero becomes a one and every one becomes a zero. Simple!

## >: Shift Right

This instruction shifts every bit in A one place to the right. The leftmost bit becomes a zero and the rightmost bit is discarded.

## <: Shift Left

This instruction shifts every bit in A one place to the left. The rightmost bit becomes a zero and the leftmost bit is discarded.

## @: Swap Nybbles

This instruction swaps the top four bits of A with the bottom four bits. For example, If A is 01101010 and you execute @, A will be 10100110:

 ____________________
|                  |
0110 1010    1010 0110
|_______|


That's all the instructions! Simple, right?

# Rules

• Your program must accept input once at the beginning. This will be a line of code. This is not an interactive interpreter! You can only accept input once and do not have to loop back to the start once that line has been executed.
• Your program must evaluate said input. Every character that is not mentioned above is ignored.
• Your program should then print out the final value of the accumulator, in decimal.
• Usual rules for valid programming languages apply.
• Standard loopholes are disallowed.
• This is , smallest byte count wins.

Here are some small programs to test out your submissions. Before the arrow is the code, after it is the expected result:

• ! -> 255
• !>> -> 63
• !<@ -> 239
• !nop!&6*! -> 255

Enjoy!

• I'm presuming from ! -> 255 that we're to use 8 bits per byte here? The question isn't explicit. – Toby Speight Sep 9 '15 at 12:10
• @TobySpeight A byte, by definition, is 8 bits. – HyperNeutrino Feb 15 '16 at 0:30

# Forth (gforth), 217

: o over ; : x 0 begin key dup emit swap o 13 = if . exit else o 33 = if invert else o 62 = if 1 rshift else o 60 = if 1 lshift else o 64 = if dup 4 lshift swap 4 rshift or then then then then then nip 255 and again ;


Not as short as I had hoped but oh well. I'm sure this can be golfed further. There doesn't seem to be any alternatives to if/elseif :/

Formatted / ungolfed:

: x
0 ( accumulator )
begin
key dup emit swap ( read char, print it and stash it away )
over 13 = if . exit ( on newline, print accumulator and return. )
else over 33 = if invert ( inversion )
else over 62 = if 1 rshift ( shift 1 right )
else over 60 = if 1 lshift ( shift 1 left )
else over 64 = if dup 4 lshift swap 4 rshift or ( swap )
then then then then then
nip ( remove read character )
255 and ( constrain accumulator to 1 byte after each step )
again
;