# 2.9. Binary Numbers¶

## 2.9.1. Introduction and Goals¶

Binary numbers are used to represent all computer data. That is, everything is in 0s and 1s. In this lesson, we'll explore the binary number system and learn how to count in binary.

Learning Objectives: I will learn to
• explain how data can be represented in bits
• explain the consequences of using bits to store data
• calculate the binary (base 2) equivalent of a positive integer (base 10) and vice versa
• compare and order binary numbers
Language Objectives: I will be able to
• describe the reasons why computers use the binary number system using target vocabulary, supporting details and examples
• explain the steps for converting between the decimal and binary number systems using key vocabulary such as binary, and positional number system out loud and in writing, with the support of vocabulary notes from this lesson

## 2.9.2. Learning Activities¶

### Video: Counting in Binary

Do you get the joke at the beginning this lesson? If not, you'll certainly get it after watching this video from the Computer Science Unplugged, which illustrates how the binary number system works.

### Activities: Decimal and Binary Odometers

Your teacher may choose to have you do some or all of the activities below:
1. Your class can act out the video above by printing and using the following Binary Dot Cards. Have 5 students volunteer to hold the 5 cards at the front of the class. Have them flip the cards randomly and see if the class can figure out the binary and the corresponding decimal number they make by counting the dots. See if the 5 volunteers can count up from 0 to 11111. What's the largest number that you can make with the 5 binary digits? You may want to return to this activity when you get to the converting binary to decimal and converting decimal to binary sections below.
2. As you saw in the video, the children together were simulating a binary odometer to count in base 2. Humans use the decimal number system, counting in base 10, probably because we have 10 fingers, but computer circuits only have 2 states, on and off, and so use the binary (base 2) number system. A binary odometer is similar to a decimal (base 10) odometer, like the one we have in our cars, except it only has two digits. And the rightmost digit is the 1s place. The digit to its left is the 2s place and then comes the 4s place and so on.

To try this yourself, use a piece of paper or the first table in this binary/hex worksheet and the odometer approach to write out the values of the first 16 binary numbers. Remember you can only use 0's and 1's. HINT: You'll need 4 digits (bits) to represent the numbers 0 through 15 in binary, so write the value 0 as 0000. If you get stuck or to check your answer, use this binary odometer app for help or to check your answer:

### Video: Converting Binary to Decimal

As you saw in the video, the binary number system is a positional number system. The value of a particular digit depends on its place. After you've watched the video, there's an activity to give you some practice at converting binary (base 2) to decimal (base 10).

In this next short video, you'll learn a simple algorithm for converting a binary number into a decimal.

### Activities: Converting Binary to Decimal

Your teacher may choose to have you do some or all of the activities below:

1. Your class can act out binary conversion using the Binary Dot Cards. Have the 5 students flip the dot cards randomly to make a binary number, and have the class figure out what decimal number it is.
2. Try the following Binary Converter. In pairs, have one partner click on the binary digits below to create a binary number, and have the other partner figure out the number as a decimal number (click on the ? button to check your the answer). You can also use the paper binary converter tool at the bottom of the binary/hex worksheet that you may have printed out in the last activity. Make sure it is printed double-sided and cut the 1's into tabs that can be flipped over to cover the 0's.
3. Use the algorithm described in the video with this interactive Khan Academy component to convert binary to decimal.

### Video: Converting Decimal to Binary

You can a similar algorithm to convert decimal numbers into binary. This next short video shows you how.

### Activities: Converting Decimal to Binary

Your teacher may choose to have you do some or all of the activities below:

1. Your class can act out binary conversion using the Binary Dot Cards. Have the class call out a decimal number and have the 5 students with the binary dot cards figure out the equivalent binary number to show.
2. Try the Binary Converter in reverse. In pairs, have one partner call out a number in base 10 from 0 to 255 (remember that the highest number that you can make with x bits is 2x - 1), and have the other partner click on the binary digits below to figure out that number in binary.
3. Complete the first two columns in the second page of the binary/hex worksheet that you may have printed out in the last activity. (Skip over rows that do not have anything written in the decimal or binary columns. You will finish the hex column of this worksheet in the next section). If you print this out double-sided, you can cut the 1's into tabs and flip them over to create a paper binary converter tool to help you with your calculations.
4. Use the algorithm described in the video with this interactive Khan Academy component to convert decimal to binary.

Everything in a computer is represented with sequences of bits, 0’s and 1’s. There are some consequences of using of using bits to represent data though. How do you think repeating decimal (real) numbers like 3.33333333… are stored in a computer’s memory? Would it fit in memory if it was an infinitely repeating decimal? Since the memory would not be able to hold an infinitely repeating number like this, computers round off repeating decimals after a fixed number of bits and only hold their approximate values in memory. Even with integer numbers, the memory will limit the size of the integer. The fixed number of bits used to represent numbers limits the range of values and mathematical operations on those values, and can even cause errors such as overflow or rounding off errors. An overflow error occurs when a computer attempts to handle a number that is larger than the memory it has available. Even modern computers can occasionally have an overflow error when the computer attempts to handle a very large number that is outside of the defined range of values can be represented. In many programming languages, integer numbers are limited to a size of 4 bytes (32 bits where each byte is 8 bits) in memory and real numbers with decimal points to 8 bytes. Languages like MIT App Inventor and the AP pseudocode only limit the size of the data by the size of the computer’s memory.

### Other Number Systems

One problem with binary numbers is that it takes lots of digits to represent relatively small numbers. For example, a number like 1 million would require 20 binary digits: 11110100001001000000. In computer science, we also use the octal (base 8) and hexadecimal (base 16) number system, which uses the digits 0-9 as well as the letters A-F to represent the decimal numbers 0-15. Each 4 binary bits can be replaced by 1 hex digit. The AP CSP exam no longer covers the hexadecimal number system, but if you're curious, you can learn more about octal and hex in the links provided in the Still Curious section below.

 Decimal Hexadecimal Binary 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111

Did you know that the Ancient Maya Civilization used a base 20 number system with just 3 symbols for 0, 1, and 5? Try the Maya Math Game. ## 2.9.3. Summary¶

In this lesson, you learned how to:

## 2.9.4. Still Curious?¶

• Learn about Octal (base 8) and Hexadecimal (base 16) number systems which are also used in computer science: video (Teacher Tube version), slides, hexodometer, binary/hex worksheet.
• Play the 2048 game to practice the powers of 2.
• Play the Binary Tetris game to practice binary/decimal conversions.
• Will YouTube ever run out of Video IDs? Every YouTube video has a unique ID that looks something like IWV2e8KDQwTM. What you may not have realized is that the ID is a number is some base. It's not base-2 or base-10 or even base-16. To find out what base YouTube uses, check out this interesting video.
• More Fun With Odometers: Here's a multiple number systems odometer app that will let you experiment with number systems in any base from 2 to 32.

## 2.9.5. Self-Check¶

### Vocabulary

Here is a table of the technical terms we've introduced in this lesson. Hover over the terms to review the definitions.

 binary sequence bit base positional number system decimal number system binary number system octal number system hexadecimal number system overflow error