Skip to content

Instantly share code, notes, and snippets.

@icylace
Last active November 30, 2025 07:47
Show Gist options
  • Select an option

  • Save icylace/3ccfaf0668a11371387de4553c011602 to your computer and use it in GitHub Desktop.

Select an option

Save icylace/3ccfaf0668a11371387de4553c011602 to your computer and use it in GitHub Desktop.
Numbers and bit width
Here are numbers that can fit within three bits:
0 -> 000
1 -> 001
2 -> 010
3 -> 011
4 -> 100
5 -> 101
6 -> 110
7 -> 111
However, our 2-bit CPU can only handle two bits at
a time. So, for each number the best we can do is
keep as many bits as possible and discard the rest.
0 -> 00
1 -> 01
2 -> 10
3 -> 11
4 -> 00 <-- Notice the "loop back".
5 -> 01
6 -> 10
7 -> 11
Above, we've kept the rightmost ("least significant")
bits because doing so lets us align our number
sequence to the looping behavior of Math's
modulo operator.
0 mod 4 = 0
1 mod 4 = 1
2 mod 4 = 2
3 mod 4 = 3
4 mod 4 = 0 <-- Again, notice the "loop back".
5 mod 4 = 1
6 mod 4 = 2
7 mod 4 = 3
Where does the 4 in "mod 4" come from? It comes from
taking the number base for binary, which is 2, and
raising that by the bit width of our CPU, which is 2.
So, 2^2 = 4, which is the total amount of distinct
bit patterns our 2-bit CPU can recognize.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment