gg Gary Garot learn / binary

Exploring CS · AP CSP

Binary Bits

Computers store every number as a row of bits, each one on (1) or off (0). Each bit is worth double the one to its right. Add up the bits that are on and you have the number.

=

Keyboard: number keys flip bits, counting from the left · Enter next number

Think about it

What is the biggest number 4 bits can hold? 8 bits?

Turn every bit on and read the total.

Answer
4 bits: 15 (1111). 8 bits: 255 (11111111). In general, n bits go up to 2n − 1.

How many different numbers can 8 bits show?

Careful: don’t forget zero.

Answer
256 — every number from 0 to 255. Each extra bit doubles the count, so n bits give 2n possible values.

How can you tell if a binary number is odd just by looking at it?

Every place value except one is even.

Answer
Look at the rightmost bit. It’s worth 1, and it’s the only odd place value, so the number is odd exactly when that bit is on.

In 8-bit mode, turn on all eight bits. What happens if you add 1 and there is no ninth bit?

Try adding 1 to 11111111 the way you would carry in decimal.

Answer
Every bit carries over and becomes 0, and the carry has nowhere to go. The computer is left with 00000000 — zero. This is an overflow error: the number got too big for the bits set aside to store it. It shows up on the AP CSP exam.