AP Computer Science Principles · Unit 2 lecture · Part 1 of 11 · 6 min

2.1 Binary numbers: bits and abstractionEverything is ones and zeros

By the end: Represent numbers, text, colors and sound in binary, explain overflow, round-off and sampling, choose between lossless and lossy compression, and use programs to clean, filter, transform and visualize data while avoiding false conclusions about cause and bias.
Mr. Thompkins
Welcome to Unit 2. Here's a strange fact to start with: your favorite song, a photo of your friends, and this sentence are all stored the same way inside a computer, as long rows of ones and zeros. By the end of today you'll know exactly how.

A computer is built from billions of tiny switches, and each switch has only two states: on or off. We write those states as 1 and 0. One of these two-state values is a bit, short for binary digit. Bits are the only thing a computer can store, so everything it handles, numbers, letters, colors, sounds, must be turned into patterns of bits.

A single bit can only say yes or no, so computers group bits together. A group of 8 bits is a byte. The bigger units you know from file sizes are just lots of bytes: a kilobyte is about a thousand bytes, a megabyte about a million, a gigabyte about a billion.

?Wait, what does "abstraction" mean in this course?
Abstraction is reducing complexity by focusing on the main idea: hiding the details that don't matter right now and keeping the ones that do. When you see the letter H on screen, you don't think about the eight switches behind it. That's abstraction at work, and the course uses the word in every unit.

Bits are grouped to represent abstractions: numbers, characters, colors and more. And here is the idea the exam loves: the same sequence of bits may represent different types of data in different contexts. The bits themselves carry no meaning. The program reading them decides whether they are a number, a letter, or part of a picture.

01001000the same 8 bits, read three waysnumber: 72letter: Hgray level 72as a whole numberin the ASCII codeas a pixelcontext decides the meaning
One byte, 01001000. Read as a whole number it is 72. Read with the ASCII code for text it is the letter H. Read as a pixel it is a dark gray.
?Wait, what's ASCII?
ASCII is a standard code that assigns a number to each character: 65 is A, 72 is H, 97 is a lowercase a, and so on. Text is stored as those numbers, in binary. You don't need to memorize ASCII values; the exam gives you any code it uses.

Colors work the same way. A screen pixel usually mixes red, green and blue light, and each of the three gets one byte, a number from 0 to 255. So one pixel takes 24 bits. Pure red is 255, 0, 0. A warm orange is 255, 165, 0. The picture on your screen is millions of these little three-byte recipes.

Pause and think
A file contains the byte 01000001. A friend says it's the number 65; another says it's the letter A. Who is right?
I thought about it. Show me
Both could be. The same bits can stand for a number or a character. Which one it is depends on how the program interprets them: a calculator would read 65, a text editor would show A.
Same bits, different meaning
A sensor stores readings as bytes. One reading is 01001000. (a) What whole number is it? (b) A text program opens the same file and shows a letter. Which letter, if 65 is A and the letters go in order? (c) What does this tell you about bits?
  1. (a) Add the place values where there is a 1: 01001000 has 1s in the 64s place and the 8s place, so 64 + 8 = 72.
    Why?
    The next section teaches place values in full. Here, the places from the left are 128, 64, 32, 16, 8, 4, 2, 1.
  2. (b) A is 65, B is 66, and so on. 72 is 7 more than 65, so count 7 letters past A: B, C, D, E, F, G, H. It's H.
  3. (c) The same sequence of bits means different things in different contexts. The program decides how to interpret them.
    Why?
    This is exactly the sentence the course uses, and it shows up in multiple-choice questions.

Answer: (a) 72. (b) H. (c) The same bits can represent different kinds of data; the meaning comes from the context.

Stuck? Get a hint
  1. Which places hold a 1? Count from the right: 1, 2, 4, 8, 16, 32, 64, 128.
  2. The 1s are in the 64 and 8 places.
  3. For the letter, count up from A = 65 to 72.
Check yourself
Which statement about bits is true?
  1. AA given sequence of bits always represents the same number.
  2. BBits can represent only numbers, so text must be stored separately.
  3. CThe same sequence of bits can represent a number, a character or a color, depending on how a program interprets it.
  4. DA byte is 2 bits.
Show the answer

C. Bits have no built-in meaning: context decides whether they are a number, a letter or a pixel. Text is stored as bits too, using a code like ASCII, and a byte is 8 bits.

  • A bit is a binary digit, 0 or 1. A byte is 8 bits.
  • Bits are grouped to represent abstractions such as numbers, characters and colors.
  • The same sequence of bits can mean different things in different contexts.
  • A typical pixel uses 24 bits: one byte each for red, green and blue.