Skip to content

Instantly share code, notes, and snippets.

@pepplejoshua
pepplejoshua / decode16.md
Last active January 5, 2024 06:07
Decoding sections of a 16 bit instruction using bit manipulation

I wrote this throwaway piece of code while working on lc-3 vm:

#include <iostream>
#include <cassert>
#include <bitset>

using namespace std;

void decode(int a) {
@pepplejoshua
pepplejoshua / shify.md
Last active January 1, 2024 06:41
Left shift and right shift operators (<< and >>)

Implementing LC-3 in zig and it seems like a good time to take some notes on how they work (or how to intuitively tell what they are doing.

Left shift

From my understanding, the form a << b can get turned into a * (2 ^ b) mathematically. So 1 << 2 becomes 1 * (2 ^ 2), which is 4.
From a more natural view, taking 1 to binary with 5 bits gives 00001. If we say shift it left 2 times, it will become 00100, which is 4. Very nice.

Right shift

The form a &gt;&gt; b can get turned into quotient(a / (2 ^ b)) mathematically. So 14 &gt;&gt; 2 becomes quotient(14 / (2 ^ 2)), which is 3. 14 / 4 is 3.5, the quotient of which is 3.