  ## Binary

### Introduction

Binary is a number system that only uses two different numbers, these are are 0 and 1.

In an electronic circuit the number 0 and the number 1 can easily be represented by a voltage being OFF or ON. Binary is ideal for use with digital circuits which are either ON or OFF. In a digital circuit ON represents Binary 1 and OFF represents Binary 0.

### 1 Bit Binary

The first two numbers, Zero and One, in our usual number system (decimal) are shown below:

Decimal Zero is represented by binary 0, Decimal One is represented by binary 1 ... or in short hand:
0 → 0 ... zero lots of 1
1 → 1 ... one lot of 1

### 2 Bit Binary

To represent Two and Three, more digits are needed. To do this there is a second column in binary:

2 → 1 0 ... one 2 and zero lots of 1 (1×2 + 0×1)
3 → 1 1 ... one 2 and one 1 (1×2 + 1×1 = 2 + 1 = 3)

### 3 Bit Binary

To represent the next numbers (Four to Seven) another column is required.

If the first, right hand, column represents the number of 1s, the second column represents 2s then the third, left hand, column represents the number of 4s:

4 → 1 0 0 ... one 4, zero 2s and zero 1s
5 → 1 0 1 ... one 4, zero 2s and one 1 (1×4 + 0×2 + 1×1 = 4 + 1 = 5)
6 → 1 1 0
7 → 1 1 1

Three columns can represent eight decimal numbers (0 → 7) because there are eight different combinations of 1s and 0s in three columns. Notice 23 = 8.

### 4 Bit Binary

It would be better if all the binary numbers had the same number of digits (called bits) and four bits are needed to count to 9. The right hand bit is called the Least Significant Bit (LSB) and is worth 1. The left hand bit is called the Most Significant Bit (MSB) and is worth 8.

The first ten decimal numbers are:

0 → 0 0 0 0
1 → 0 0 0 1
2 → 0 0 1 0
3 → 0 0 1 1
4 → 0 1 0 0
5 → 0 1 0 1
6 → 0 1 1 0
7 → 0 1 1 1
8 → 1 0 0 0 ... one 8 and nothing else
9 → 1 0 0 1

Not all of the combinations have been used yet because 24 = 16 and so, with four bits, 16 numbers can be represented.

10 → 1 0 1 0
11 → 1 0 1 1
12 → 1 1 0 0 ... one 8 and one 4
13 → 1 1 0 1
14 → 1 1 1 0
15 → 1 1 1 1 ... 1×8 + 1×4 + 1×2 + 1×1 = 15

The biggest number represented is 15 because to represent 16 requires a 5th bit. 25 = 16 so the 5th bit is worth 16.

### More Bits

Bigger numbers are represented by more bits. Each bit is worth twice the previous bit. Therefore the first few bits in a binary number are worth:

1st bit → 20 = 1
2nd bit → 21 = 2
3rd bit → 22 = 4
4th bit → 23 = 8
5th bit → 24 = 16
6th bit → 25 = 32
7th bit → 26 = 64
8th bit → 27 = 128
9th bit → 28 = 256
10th bit → 29 = 512
11th bit → 210 = 1024

### Converting Decimal to Binary

First decide how many bits are necessary and then decide which bits are necessary to add up to the desired decimal number.

Example: What is 27 in binary?
Solution: The 6th bit is worth 32 and that is more than needed therefore use 5 bits.
5th bit → 16. Is it needed? Yes. 27 − 16 = 11 remaining
4th bit → 8. Is it needed for the remaining 11? Yes. 11 − 8 = 3 remaining
3rd bit → 4. Is it needed? No, only 3 remaining, don't need a 4
2nd bit → 2. Is it needed for 3? Yes. 3 − 2 = 1 remaining
1st bit → 1. Is it needed? Yes

Therefore 27 → 1 1 0 1 1
(1×16 + 1×8 + 0×4 + 1×2 + 1×1)

Example: What is 42 in binary?
Solution: 6 bits are required. 7th bit → 64 which is too big.
1 × 32 ... 42 − 32 = 10
0 × 16
1 × 8 ... 10 − 8 = 2
0 × 4
1 × 2 ... 2 − 2 = 0
0 × 1

Therefore 42 → 1 0 1 0 1 0

Note, sometimes it is necessary to represent numbers as 8 bit binary numbers. In this case just add extra zeros to the left hand side as these don't add any value:

27 → 0 0 0 1 1 0 1 1
42 → 0 0 1 0 1 0 1 0

### Converting Binary to Decimal

To convert a binary number to decimal, add up each digit that is none zero.

Example: What is 0 1 1 1 0 0 0 1 in decimal?
Solution: The MSB is worth 128 as this is an 8 bit binary number (27 = 128)
From the left we have:
0×128 + 1×64 + 1×32 + 1×16 + 0×8 + 0×4 + 0×2 + 1×1 = 113

0 1 1 1 0 0 0 1 → 113