# Binary

Binary or base 2 is the number system used natively by computers. It uses only the digits 0 and 1. It is useful in ROM hacking especially when a value actually consists of multiple values, which do not take a multiple of eight bits each. For example, the SNES color format consists of fives bits for each of the red, green, and blue color components in a word.

More non-ROM hacking specific information can be found at Wikipedia's binary article.

## Terminology

### Bit

Each digit in a binary number is referred to as a bit.

More non-ROM hacking specific information can be found at Wikipedia's bit article.

### Nybble

A nybble or nibble is a group of four bits. Most computers cannot read or write single nybbles, but they are notable since every hexadecimal digit represents exactly one nybble.

### Byte

A byte is a group of eight bits. This is the smallest grouping which most computers can work with directly. Some formats may involve smaller divisions like the SNES color format mentioned above, but special code must be used to deal with this.

More non-ROM hacking specific information can be found at Wikipedia's byte article.

### Word

A word is the natural unit of data for a given architecture. Unlike the previous terms, the size of a word varies from system to system. A word is usually a power of two time the size of a byte - generally 16 bits (two bytes), 32 bits (four bytes) or 64 bits (8 bytes). For example, a word on a GBA is 32 bits, whereas on a NES it is 16 bits. The order of these bits depends on the endianness of the machine. From this term, comes halfword (hword), for a unit of data half of the word size, double word (dword) for double the word size and quadruple word (qword) for eight times the word size.

More non-ROM hacking specific information can be found at Wikipedia's Word_(computer_science) article.

## Working With Binary Values

### Converting a Decimal Value to a Binary Value

Although many rom hackers can do this conversion in their head, it is very important for all of us to be able to convert decimal values to binary values.

This can be done manually or using a calculator like Windows Calculator.

Please note that this method does not cover floating point decimals, which are rare in ROM hackering.

#### Using Paper and Pencil

First, write the following table on any piece of paper:

```| 2^7 | 2^6 | 2^5 | 2^4 | 2^3 | 2^2 | 2^1 | 2^0
```

Then under the 2^7, write the non-exponential eqivalent, 128. Repeat for all of them, remembering that 2^0 is 1.

Now, the process is very simple. First, pick the decimal value you want to convert. Start from the left of the table. Now ask yourself, "If I take my number and take away 2^7 (128) from it, will it still be a positive number?" If yes, then do the subtraction and mark a 1 under the 2^7 on the table. Now, save the result from the previous subtraction and ask the same question to the next exponential 2^6 = 64. If it's true, then write another 1 in the table. If, by any chance, you get to a number where you cannot do the subtraction without getting a negative number as the answer, just write a 0 on the table and go on to the next number. When you are done with 2^0 = 1, you are all done. Results of DEC to BIN conversions:

In the following examples, note that if the number on the table is 0, then you do not count its exponential towards the final sum.

```DEC - 217
BIN - 11011001 (128 + 64 + 0 + 16 + 8 + 0 + 0 + 1)
```
```DEC - 128
BIN - 10000000 (128 + 0 + 0 + 0 + 0 + 0 + 0 + 0)
```
```DEC - 127
BIN - 01111111 (0 + 64 + 32 + 16 + 8 + 4 + 2 + 1)
```

#### Using the Windows Calculator

I know many of you may be thinking, "What???? This guys is talking about the M\$ 0\$!". Well, I picked the M\$ Window\$ calculator because it's a widely used PC calculator. Linux and other operating systems usually come with a bundled scientific calculator, and all of them usually work basically the same way.

Without further ado, Here's how to:

1. Open the windows calculator (Goto Start -> Run -> Type in calc -> Press Enter)

2. On the View menu, click Scientific.

3. Now click on the DEC radio button.

4. Now enter the DEC value you want to convert.

5. Now click the BIN radio button.

6. The display should be showing the BIN equivalent to the DEC number you entered previously!!!

7. Repeat steps 3-6 to convert more numbers!

Remember, if you enter a number that's less than 128, it will only show from the 1st 1!

Ex. If you enter 72, you'll get 1001000, which is the same as 010010000.

I told you that using the calculator is easier, lol!