# 2.9. Binary Numbers¶

### Time Estimate: 90 minutes

## 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¶

### There are only 10 types of people in the world. Those who understand binary and those who don't!

- slides
- |
- YouTube video Part 1
- |
- TeacherTube video Part 1
- |
- YouTube video Part 2
- |
- TeacherTube video Part 2
- |
- YouTube video Part 3
- |
- TeacherTube video Part 3
- |
- Binary Dot Cards
- |
- worksheet

### Lecture

### 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:- 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.
- 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:

- 5
- This will be a challenging concept to learn, but we can all reach this goal. The number 8 in binary is represented as 1000. Additional numbers can be represented by turning some of the 0s into 1s. For example, the number 9 would be represented as 1001.
- 12
- This will be a challenging concept to learn, but we can all reach this goal. The number 12 in binary is represented as 1100. Additional numbers can be represented by turning some of the 0s into 1s. For example, the number 13 would be represented as 1101.
- 15
- Yes. The largest number that can be represented in 4 bits would be 1111, which is 1 + 2 + 4 + 8, which equals 15.
- 16
- This will be a challenging concept to learn, but we can all reach this goal. To represent 16 in binary, you would need 5 bits. It's representation is 10000.

Q-3: What’s the largest number that can be represented in 4 bits?

- Trying to represent 15 in 4 bits.
- This is challenging, but rewarding! This would not cause an overflow. 15 is represented as 1111 in 4 bits.
- Trying to represent 16 in 4 bits.
- Yes. The largest number that can be represented in 4 bits is 15, which is 1111. To represent 16 you would need an additional bit, 1 0000.
- Trying to represent 31 in 5 bits.
- This is challenging, but rewarding! This would not cause an overflow. 31 is represented as 1 1111 in 5 bits.
- Trying to represent 32 in 5 bits
- Yes. The largest number that can be represented in 5 bits is 31, which is 1 1111. To represent 32 you would need a 6th bit, 10 0000.

Q-4: An overflow error occurs when there aren’t enough bits to represent a given number. Which of following would cause an overflow error to occur. (Choose all that apply.)

- True.
- Yes, to think of a simple example, if you were using 4 bits to represent numbers you wouldn't be able to represent the number 17, so you wouldn't be able to add 17 + 8. Modern computers use 32 or 64 bits to represent whole numbers, which are called
*integers*. With 32 bits, you can represent 2^{32}different values. That's 4,294,967,296, more than 4 billion values. But, of course, there are still numbers that would cause overflow errors -- e.g., 4,300,000,000 could not be represented using 32 bits. This potential for overflow error is a necessary implication of using arepresentation to model the**finite**concept of number.**infinite** - False
- This is challenging, but rewarding! Using a fixed number of bits to represent numbers does limit the range of problems you can solve. For example, if you were using 4 bits to represent numbers you wouldn't be able to represent the number 17, so you wouldn't be able to add 17 + 8. Modern computers use 32 or 64 bits to represent whole numbers, which are called
*integers*. With 32 bits, you can represent 2^{32}different values. That's 4,294,967,296, more than 4 billion values. But, of course, there are still numbers that would cause overflow errors -- e.g., 4,300,000,000 could not be represented using 32 bits. This potential for overflow error is a necessary implication of using arepresentation to model the**finite**concept of number.**infinite**

Q-5: True or False. Using a fixed number of bits to represent numbers limits the range of values and hence limits the range of problems that can be solved with that representation.

### 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:

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

- 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.
- 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 2
^{x}- 1), and have the other partner click on the binary digits below to figure out that number in binary.

- 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.
- 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 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|

Hexadecimal | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |

Binary | 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
. 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.*IWV2e8KDQwTM* - 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 |

### Check Your Understanding

Complete the following self-check exercises.

- 1001 0100
- 1001 0111
- 1101 0100
- 1101 0111

Q-10: AP 2021 Sample Question: Each student that enrolls at a school is assigned a unique ID number, which is stored as a binary number. The ID numbers increase sequentially by 1 with each newly enrolled student. If the ID number assigned to the last student who enrolled was the binary number 1001 0011, what binary number will be assigned to the next student who enrolls?

Reflection: For Your Portfolio

Answer the following portfolio reflection questions as directed by your instructor. Questions are also available in this Google Doc where you may use File/Make a Copy to make your own editable copy.