Youtube
Binary Numbers
Humans use a base 10 number system for counting numbers, most likely because have 10 fingers. For example, using your fingers to do 8 + 4
, since we don’t have 11 fingers we close our hands and have to hold a 10 in our mind, reset our fingers and add two: + 2 = 12.
This number system is natural for us but imagine a dolphin counting with it’s 2 fins. 1 + 2 = 3, but they don’t have 3 fins so they have to hold in their mind increments of 2. Computers are just like this in that they use a base 2 system(aka binary) of couting in contrast to our base 10(decimal).
Recall(in the language chapter) that 4 bits gives us a maximum of 16 unique combinations because (24 = 16).
Decimal | Binary |
---|---|
0 | 0000 |
1 | 0001 |
2 | 0010 |
3 | 0011 |
4 | 0100 |
5 | 0101 |
6 | 0110 |
7 | 0111 |
8 | 1000 |
9 | 1001 |
10 | 1010 |
11 | 1011 |
12 | 1100 |
13 | 1101 |
14 | 1110 |
15 | 1111 |
Every 1 toggles corresponding powers of two:
23 | 22 | 21 | 20 |
---|---|---|---|
0 | 0 | 0 | 0 |
If we need more to represent numbers over 16, we need to expand to 5 bits, 25 = 32
24 | 23 | 22 | 21 | 20 |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
#How to convert a binary number to a decimal number?
Look at where there is a 1
8 4 2 1 0 1 1 1 Notice there are 1’s underneeth the 4,2, and 1 headers
Sum up the corresponding power of 2 number
you can think of it as an on/off switch
8 4 2 1 0 1 1 1 0 + 4 + 2 + 1 = 7; thus our answer is 0111 represents 7
Extra Examples
8 | 4 | 2 | 1 |
---|---|---|---|
0 | 1 | 0 | 0 |
0 + 4 + 0 + 0 = 4; thus 0100 represents 4.
8 | 4 | 2 | 1 |
---|---|---|---|
1 | 0 | 1 | 0 |
8 + 0 + 2 + 0 = 10; thus 1010 represents 10.
Check your understanding!
What is the binary number 0011 in decimal?
8 | 4 | 2 | 1 |
---|---|---|---|
0 | 0 | 1 | 1 |
answer: 3; because 2 + 1 = 3
What is the binary number 0101 in decimal?
8 | 4 | 2 | 1 |
---|---|---|---|
0 | 1 | 0 | 1 |
answer: 5; because 4 + 1 = 5
What is the binary number 1001 in decimal ?
8 | 4 | 2 | 1 |
---|---|---|---|
1 | 0 | 0 | 1 |
answer: 9; because 8 + 1 = 9
#How to convert a decimal number to a binary number?
Subtraction method
Find the biggest power of 2 that is less than the decimal number and place a 1 in that position
Let’s try converting the number 138 4 2 1 1 0 0 0 8 is the biggest power of 2 that is less than 13, hence we put a 1 there.
Subtract it from the original number and repeat step 1 until you subtract to 0
13-8 = 5
8 4 2 1 1 1 0 0 Now we need to look for the next biggest power of 2 that is less than 5, which is 4, hence we put a 1 there.
5 - 4 = 1
Lastly 1 fits with 1, so 1 - 1 = 0, and we are done.
8 4 2 1 1 1 0 1 8+4+0+1 = 13; thus 1101 represents 7
Check your understanding!
What is the decimal number 11 in binary?
8 | 4 | 2 | 1 |
---|---|---|---|
1 | 0 | 1 | 1 |
answer: 1011; because 8 + 2 + 1 = 11
What is the decimal number 15 in binary?
8 | 4 | 2 | 1 |
---|---|---|---|
1 | 1 | 1 | 1 |
answer: 1111; because 8 + 4 + 2 + 1 = 15
What is the decimal number 233 in binary?
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|---|---|---|
1 | 1 | 1 | 0 | 1 | 0 | 0 | 1 |
answer: 11101001; which you can confirm adds up to 233. Notice how we needed to expand to 8 bits to represent this number because 233 can only fit into 8 bits or higher: 28 = 256
Division method
Modulo the number by two
Let’s try for the number 233
233/2 = 116 remainder 1Recursively modulo the quotient while keeping track of the remainder
11101001 Division Quotient Remainder 233/2 116 1 116/2 58 0 58/2 29 0 29/2 14 1 14/2 7 0 7/2 3 1 3/2 1 1 1/2 0 1
Note: The remainder will always be 0 or 1Gather all the remainders from bottom to top
From bottom to top in the remainders column, 11101001, which is the binary representation of 233
Check your understanding!
Use the division method
What is 16 in binary?
Division | Quotient | Remainder |
---|---|---|
16/2 | 8 | 0 |
8/2 | 4 | 0 |
4/2 | 2 | 0 |
2/2 | 1 | 0 |
1/2 | 0 | 1 |
16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|
1 | 0 | 0 | 0 | 0 |
What is 23 in binary?
Division | Quotient | Remainder |
---|---|---|
23/2 | 11 | 1 |
11/2 | 5 | 1 |
5/2 | 2 | 1 |
2/2 | 1 | 0 |
1/2 | 0 | 1 |
16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|
1 | 0 | 1 | 1 | 1 |
What is 30 in binary?
Division | Quotient | Remainder |
---|---|---|
30/2 | 15 | 0 |
15/2 | 7 | 1 |
7/2 | 3 | 1 |
3/2 | 1 | 1 |
1/2 | 0 | 1 |
16 | 8 | 4 | 2 | 1 |
---|---|---|---|---|
1 | 1 | 1 | 1 | 0 |