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).

unsigned
DecimalBinary
00000
10001
20010
30011
40100
50101
60110
70111
81000
91001
101010
111011
121100
131101
141110
151111


Every 1 toggles corresponding powers of two:

23222120
0000


If we need more to represent numbers over 16, we need to expand to 5 bits, 25 = 32

2423222120
00000

#How to convert a binary number to a decimal number?

  1. Look at where there is a 1
    8421
    0111

    Notice there are 1’s underneeth the 4,2, and 1 headers

  2. Sum up the corresponding power of 2 number

    you can think of it as an on/off switch

    8421
    0111

    0 + 4 + 2 + 1 = 7; thus our answer is 0111 represents 7


Extra Examples
8421
0100

0 + 4 + 0 + 0 = 4; thus 0100 represents 4.

8421
1010

8 + 0 + 2 + 0 = 10; thus 1010 represents 10.


Check your understanding!

What is the binary number 0011 in decimal?
8421
0011

answer: 3; because 2 + 1 = 3



What is the binary number 0101 in decimal?
8421
0101

answer: 5; because 4 + 1 = 5



What is the binary number 1001 in decimal ?
8421
1001

answer: 9; because 8 + 1 = 9



#How to convert a decimal number to a binary number?

Subtraction method

  1. 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 13

    8421
    1000

    8 is the biggest power of 2 that is less than 13, hence we put a 1 there.

  2. Subtract it from the original number and repeat step 1 until you subtract to 0

    13-8 = 5

    8421
    1100

    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.

    8421
    1101

    8+4+0+1 = 13; thus 1101 represents 7

Check your understanding!

What is the decimal number 11 in binary?
8421
1011

answer: 1011; because 8 + 2 + 1 = 11


What is the decimal number 15 in binary?
8421
1111

answer: 1111; because 8 + 4 + 2 + 1 = 15


What is the decimal number 233 in binary?
1286432168421
11101001

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

  1. Modulo the number by two

    Let’s try for the number 233
    233/2 = 116 remainder 1

  2. Recursively modulo the quotient while keeping track of the remainder
    11101001
    DivisionQuotientRemainder
    233/21161
    116/2580
    58/2290
    29/2141
    14/270
    7/231
    3/211
    1/201


    Note: The remainder will always be 0 or 1

  3. Gather 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?
10000
DivisionQuotientRemainder
16/280
8/240
4/220
2/210
1/201
168421
10000


What is 23 in binary?
10111
DivisionQuotientRemainder
23/2111
11/251
5/221
2/210
1/201
168421
10111


What is 30 in binary?
11110
DivisionQuotientRemainder
30/2150
15/271
7/231
3/211
1/201
168421
11110