Lesson 1 - Binary Numbers

What is binary?

At their most simple level, computers are basically a collection of circuits turned on or off. We use binary to represent these circuits.

Binary is a counting system. Counting systems are the symbols used to represent numbers. The most widely used counting system is known as decimal. This is the number system that you are familiar with and use in your every day life. In decimal, there are ten symbols 0 1 2 3 4 5 6 7 8 9 used to represent different numbers (zero through nine). When we run out of symbols to represent our numbers, we add a new place value. For example, 9 is the highest symbol in the decimal number system, when we represent the number ten, we add a place value and start the one’s place at 0. 9 -> 10

Binary is a counting system that only uses two symbols to represent a number (1 and 0). We use it for computer science because it allows us to represent a semi-conductor that is turned off with a 0. We represent a semi-conductor that is turned on with a 1. You can also think of binary numbers in terms of True (1) and False (0). We call each place value that contains a 1 or a 0 a bit. 8 bits make up one byte. Modern computers are built on millions of bytes that are connected.

How to Convert between Binary and Decimal

Binary and decimal essentially mean the same thing. They are both counting systems used to represent numerical values. Whether we write the number seven as 111 (in binary) or 7 (in decimal) does not make a difference to humans. They both represent a value of seven. Computers use binary because it allows for mathematical operations to be implemented as circuits. Doing this with a decimal counting system would be impossible. In order to make binary operations more readable to humans, it is often useful to convert from binary to decimal.

Convert from Binary to Decimal.
  1. Index the binary number
    indexed binary number
    Before we convert our number, we must take a look at the indices or digit places of the binary number. We list the indices as above, with the right-most index starting at zero. Then we increase the index by one with every step to the left. Just like in decimal, as we move further to the left in our number, our digits increase in significance meaning that digits further to the left represent larger numbers.
  2. We convert binary to decimal summing d*2^i over all indices. i represents the index and d represents the value stored at the index. So we convert the number above like this:
    binary to decimal conversion
Convert from Decimal to Binary

To convert a decimal number to binary, we do the inverse of the operation above. The steps are detailed below

  1. Find the largest index. We can use logarithms for this. The largest index for any number n is equal to floor(log2(n))
    largest index formula
    The symbols that look like L’s enclosing the log operator are known as the floor operator. They simply indicate to round down to the nearest whole number. For example floor(4.5) = 4. To convert the decimal number 12 to binary first we find the largest index (L).
    largest index 12
    The value of n at this index will be 1.

  2. Convert the remaining digits to binary. We know 2^3 = 8 and we are trying to convert the number 12 to binary. In the last step, we concluded that the value at index=3 is 1. The decimal value of the number 1000 is 8. So we subtract 1*2^3 from n. This result gives us: 12-8 = 4.

  3. Represent the remaining value in binary. We must repeat steps 1-2 until our remainder is zero. So we now convert the number 4 to binary. The largest index of 4 is 2. So now we do 4 - 1*2^(2) = 0. Since this equals zero, we know we are done. So the value of 12 in binary is 1100. If our remainder was greater than zero, we would repeat steps 1 and 2 on the next indices. If the largest index of our remainder less than the next 2^i where i is our next index, we enter a 0 at index i and proceed to the next index.
    conv_d_to_b

Practice

1. Write the following decimal numbers as 4-bit binary numbers.

0
1
2
3
4
5
7
14
8

Solutions
Decimal Binary
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
7 0111
14 1110
8 1000

2. Write the following 4-bit binary numbers in decimal.

0101
1010
1111
0010
1000
1101

Solutions
Binary Decimal
0101 5
1010 10
1111 15
0010 2
1000 8
1101 13

Activity 1: Numbers as Redstone

Using what you know about binary, pick a random positive number and write it by turning Redstone torches (or Redstone Lamps) on or off. Redstone torches that are turned on represent a 1 and Redstone torches that are turned off represent a 0.
Lesson 1