# 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

It is useful to be able to convert between binary and decimal. When this calculation cannot be carried out mentally, there are simple methods of making the conversion on paper. Many operating systems also include a calculator which can convert between binary and decimal.

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

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)

## See Also

## External Links