AP® Computer Science Principles · Big Idea 2 · Topic 2.1
Converting Between Binary and Decimal
Binary and decimal are two ways to write the same number. Decimal counts in powers of ten; binary counts in powers of two, using only digits 0 and 1 for each place value.
Every number system assigns a value to each position a digit sits in. In decimal, the places are 1, 10, 100, 1000 — powers of ten. In binary, the places are 1, 2, 4, 8, 16, 32, 64, 128 — powers of two. A binary digit, or bit, can only be 0 or 1, so each place either counts fully or not at all. That is why building a binary number feels like making change using only coins worth powers of two: you decide, place by place, whether that coin fits.
To convert decimal to binary, divide repeatedly by 2 and keep track of the remainders. Each division asks whether the current amount is odd or even — the remainder, 0 or 1, becomes one bit. Reading the remainders from last to first gives the binary number. To go the other direction, binary to decimal, multiply each bit by the place value of its position and add up the results. A 1 counts that place's full value; a 0 contributes nothing.
This skill is the foundation for everything else in the data unit. Before you can talk about mixing colors with RGB values or compressing an image, you need to trust that a string of bits is really just a number written in a different base, and that the number of bits available limits how large a value can be stored. Comparing binary numbers also depends on this: more digits does not automatically mean a bigger value, since leading zeros cost nothing.
Key terms
- bit
- A single binary digit, either 0 or 1.
- place value
- The value a digit contributes based on its position, such as 1, 2, 4, 8 in binary.
- base
- The number of digits a counting system uses before it carries over to a new place, 2 for binary and 10 for decimal.
- byte
- A group of 8 bits, often used to store one value such as a color channel.
Watch it explained
- How it worksHow does dividing by 2 over and over and collecting remainders spell out a binary number?
- Worked exampleFind the binary equivalent of the decimal number 45.
- Worked exampleFind the decimal value of the binary number 11010011.
- Going furtherWhat changes about comparing two binary numbers when one has more digits than the other?
Check yourself
End of topic.