# Bits & Bytes - Counting in Binary and Decimal

*How Integers are Stored in Memory*

One of the most fundamental aspects of any digital device is the way integers are stored in memory. If you want to get down into the number bases, bits and bytes of integers, etc. then this article is for you.

# Digits and Number Bases

Just what is the "number base" of a number system? Simply put, it's the number of unique characters that represent a single digit position of a multi-digit number. For example, each decimal digit is a number between 0 - 9. Computers commonly use bits (abbreviation for "binary digit"), octal digits and hex digits.

Number System | Base | Digits |
---|---|---|

Decimal | 10 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 |

Binary | 2 | 0, 1 |

Octal | 8 | 0, 1, 2, 3, 4, 5, 6, 7 |

Hexadecimal (Hex) | 16 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F |

## Counting in Decimal (Base-10)

All readers are familiar with the decimal number system. You learned it as a small child when you learned how to count. There is a "one's digit", "ten's digit", "hundred's digit", "thousand's digit",and so on. The least significant digit goes on the right, and the digits increase in significance (i.e. value) as you move to the left. In math itself, there is no limit to the number of digit positions that can be included to the left.

You probably never stopped to think about why the digits are called the "one's digit", "ten's digit", "hundred's digit","thousand's digit", etc.. Mathematically speaking, it's really pretty straight forward: because there are ten unique characters per digit position, they represent ten (i.e base-10) raised to the power of the indexed position, starting with 0 on the right ( ... 3, 2, 1, 0).

Tousand's Digit | Hundred's Digit | Ten's Digit | One's Digit |
---|---|---|---|

10^{3} = 1000 |
10^{2} = 100 |
10^{1} = 10 |
10^{0} = 1 |

Counting follows a well-known pattern:

- There is fixed number of characters per digit (i.e. 0 - 9).
- Before you start counting, all digits start at the first character: 0
- Every time you increment, you advance the least significant digit (one's digit) by 1 character until all of the characters are used for that digit: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
- Once you have used all of the characters at that digit, you increment the next more significant digit (i.e. to the left) by 1 character, and reset the current digit's character to 0.

Hundred's Digit | Ten's Digit | One's Digit | Comments |
---|---|---|---|

0 | 0 | 0 | All digits start at zero |

0 | 0 | 1 | |

0 | 0 | 2 | |

0 | 0 | 3 | |

0 | 0 | 4 | |

0 | 0 | 5 | |

0 | 0 | 6 | |

0 | 0 | 7 | |

0 | 0 | 8 | |

0 | 0 | 9 | Last character for the one's digit position |

0 | 1 | 0 | Reset least significant digit to zero; Increment next digit to left by one |

0 | 1 | 1, 2, 3, 4, 5, 6, 7, 8 | |

0 | 1 | 9 | Last character for one's digit position |

0 | 2 | 0 | Reset least significant digit to zero; Increment next digit to left by one |

0 | ... | ... | |

0 | 9 | 9 | Last character for one's digit position; Also last digit for ten's digit position |

1 | 0 | 0 | Reset one's digit and hundred's digit to zero; Increment next digit to left by one |

## Counting in Binary (Base-2)

Counting in binary follow the same rules as counting in decimal. The only difference is that in decimal there are ten characters per digit (i.e. 0 - 9), whereas in binary there are only two characters per digit (i.e. 0 - 1). Than means instead of a "one's digit", "ten's digit", "hundred's digit", etc. there is a "one's digit", "two's digit", "four's digit", "eight's digit", etc. In other words, instead of each more significant digit being a power of ten as in decimal, each more significant digit is instead a power of two in binary.

Eight's Digit | Four's Digit | Two's Digit | One's Digit |
---|---|---|---|

2^{3} = 8 |
2^{2} = 4 |
2^{1} = 2 |
2^{0} = 1 |

But, in math, counting is counting regardless of number base, so counting in binary follows the same rules as counting in decimal, with the only difference being the number of characters per digit:

- There is fixed number of characters per digit (i.e. 0 - 1).
- Before you start counting, all digits start at the first character: 0
- Every time you increment, you advance the least significant digit (one's digit) by 1 character until all of the characters are used for that digit: 1
- Once you have used all of the characters at that digit, you increment the next more significant digit (i.e. to the left) by 1 character, and reset the current digit's character to 0.

Four's Digit | Two's Digit | One's Digit | Comments |
---|---|---|---|

0 | 0 | 0 | All digits start at zero |

0 | 0 | 1 | Last character for one's digit position |

0 | 1 | 0 | Reset least significant digit to zero; Increment next digit to left by one |

0 | 1 | 1 | Last character for one's digit position; Also last character for two's digit position |

1 | 0 | 0 | Reset one's digit and two's digit to zero; Increment the next digit to the left by one |

Of course, with a total of ten fingers, you could actually count from 0 -1023 in binary using your fingers!

512 | 256 | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|---|---|---|---|---|

1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |

Counting in binary on your fingers is not very practical, though; I am pretty sure most people are not dexterous to make all 1024 (0 - 1023) of the finger combinations. However, binary is particularly well-suited for computers which uses transistor that can be switched into one of two states: "on" or "off".

# Bits & Bytes

By now, you should understand that the value of a number is simply the digit at a specific place position times the value of the place position. Consider, for example, the decimal number 9876. That is equal to

9 * 10^{3} + 8 * 10^{2} + 7 * 10^{1} + 6 * 10^{0} = 9 * 1000 + 8 * 100 + 7 * 10 + 6 * 1 = 9000 + 800 + 70 + 6 = 9876

10^{3} = 1000 |
10^{2} = 100 |
10^{1} = 10 |
10^{0} = 1 |
---|---|---|---|

9 | 8 | 7 | 6 |

Binary is no different. Consider the number binary number 1001. The decimal value of that binary number can be computed the same way:

1 * 2^{3} + 0 * 2^{2} + 0 * 2^{1} + 1 * 2^{0} = 1 * 8 + 0 * 4 + 0 * 2 + 1 * 1 = 8 + 0 + 0 +1 = 9

2^{3} = 8 |
2^{2} = 4 |
2^{1} = 2 |
2^{0} = 1 |
---|---|---|---|

1 | 0 | 0 | 1 |

Binary digits, or bits, are commonly grouped into packs of bits:

Number of bits | Nomenclature |
---|---|

1 | bit |

4 | Nibble |

8 | Byte |

Since a Nibble is 4 bits, that means that a nibble can represent binary values from 0000 to 1111. That is where hexadecimal comes in. Since hexadecimal is base-16, that means it has 16 different symbols (0 - 9, A -F) so if fits perfectly with a Nibble.

Binary | Hexadecimal | Decimal |
---|---|---|

0000 | 0 | 0 |

0001 | 1 | 1 |

0010 | 2 | 2 |

0011 | 3 | 3 |

0100 | 4 | 4 |

0101 | 5 | 5 |

0110 | 6 | 6 |

0111 | 7 | 7 |

1000 | 8 | 8 |

1001 | 9 | 9 |

1010 | A | 10 |

1011 | B | 11 |

1100 | C | 12 |

1101 | D | 13 |

1110 | E | 14 |

1111 | F | 15 |

Therefore, a Byte, which is an 8-bits, can also be represented by two hexadecimal digits between 00 - FF, which has a decimal value of 0 - 255.

Binary | Hexadecimal | Decimal |
---|---|---|

0000 0000 | 00 | 000 |

0000 0001 | 01 | 001 |

0000 0010 | 02 | 002 |

... | ... | ... |

0000 0100 | 04 | 004 |

... | ... | ... |

0000 1000 | 08 | 008 |

... | ... | ... |

0001 0000 | 10 | 16 |

... | ... | ... |

0010 0000 | 20 | 32 |

... | ... | ... |

0100 0000 | 40 | 64 |

... | ... | ... |

1000 0000 | 80 | 128 |

... | ... | ... |

1000 1000 | 88 | 136 |

... | ... | ... |

11111111 | FF | 255 |

Programmer, Engineer