The NES is an 8-bit system. But what is a “bit” actually? And a “byte”? And what are these weird number systems, like binary and hexadecimal, all about? Let me try to explain a bit here.
What is a bit?
You can consider a bit to be a switch. That switch can be on or off: yes or no, one or zero. This is what the binary number system uses as well: a binary digit can only be a 1 or a 0. This is the number system most computers, including the NES, use.
What is a byte?
A byte consists of eight bits: eight switches that each can be either on (1) or off (0). There are 256 possible configurations for these switches to be in, varying from all off (00000000) to all on (11111111). A single value within the NES consist of a single byte, or eight bits, which is why the NES is called an 8-bit system.
You can use a byte in various ways. You can use it for what it is: eight switches. For example, NESmaker uses screenFlags this way. This way, you can store eight switches in a single variable, which saves a lot of ROM space. You can also use a byte as a single variable between 0 and 255. The player’s horizontal position on screen is such an example.
But there are even more ways to assign values to bytes! Split a byte up in two parts and you can store two values from 0-15 in a single byte! This is how NESmaker stores screen numbers, for example. Fun fact: half a byte (or four bits) is called a Nybble. Actually, you can split up a byte in a lot of other ways as well, it doesn’t even have to be symmetrical. For example, NESmakers Object_frame variable is split in three parts: aa bbb ccc, where bbb is the current action step (0-7) and ccc is the current animation frame (0-7). These methods of storing values all contribute to saving space in your game.
Binary, decimal and hexadecimal
Reading and writing out long strings of ones and zeroes might become quite tedious after a while though. That’s why we may want to use a different number system besides binary. The first number system that might come to mind is the decimal (or Base 10) system, since we’re most used to using it in our every day lives. The downside of using the decimal system is that it might be harder to convert between binary (Base 2) and decimal. For instance, which decimal value translates to the binary value of 10110101? Spoiler: it’s 1 + 4 + 16 + 32 + 128, or 181. On first sight, there’s no obvious correlation between 181 and 10110101.
Here comes Base 16 to save the day! Base 16, also known as hexadecimal, uses sixteen digits instead of ten: 0 to 9 and A (decimal 10) to F (decimal 15). The next number after F will be 10 (or 16 in decimal), then 11, and so on, up to 1F, after which comes 20 (or 32 in decimal). The maximum value for a byte, which is 11111111 in binary or 255 in decimal, is FF in hexadecimal (or hex, for short). This correlates to binary much better than decimal does. Look at this table:
Decimal | Binary | Hexadecimal |
---|---|---|
0 | 0000 | 0 |
1 | 0001 | 1 |
2 | 0010 | 2 |
3 | 0011 | 3 |
4 | 0100 | 4 |
5 | 0101 | 5 |
6 | 0110 | 6 |
7 | 0111 | 7 |
8 | 1000 | 8 |
9 | 1001 | 9 |
10 | 1010 | A |
11 | 1011 | B |
12 | 1100 | C |
13 | 1101 | D |
14 | 1110 | E |
15 | 1111 | F |
16 | 0001 0000 | 10 |
Now check this out:
1011 in binary is B in hex.
0101 is binary is 5 in hex.
10110101 in binary… is B5 in hex!
This works for all binary strings! Split it up in groups of four, convert each group to its hex value and glue those back together. That’s much easier than converting to decimal.
Still not convinced? Try converting this binary string to decimal, then to hex:
00101101011011100101101101100010
Which one was easier to do?
This is why in code you might encounter hexadecimal values more often than decimal values. A brief introduction to variable notations in Assembly: a decimal value will be proceeded with #, a binary value with #% and a hax value with #$. This way, the complier knows how to interpret the number added. The following three lines of code all do the same:
LDA #181
LDA #%10110101
LDA #$B5
More on Assembly in another post though, as I think there’s more than enough information to digest in this post already.