Main content

## AP®︎/College Computer Science Principles

# Binary numbers

AP.CSP:

DAT‑1 (EU)

, DAT‑1.C (LO)

, DAT‑1.C.1 (EK)

, DAT‑1.C.2 (EK)

, DAT‑1.C.3 (EK)

, DAT‑1.C.4 (EK)

, DAT‑1.C.5 (EK)

📺

*Would you prefer to learn about binary numbers from video lessons? Just skip this article and continue to the videos instead.*As humans, we typically represent numbers in the

**decimal**system. Counting to ten is as simple as 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.As we just learned, computers represent all information in

**bits**. In order to represent numbers with just start text, 0, end texts and start text, 1, end texts, computers use the**binary**number system. Here's what it looks like when a computer counts to ten: start text, 0, end text, start text, 0, end text, start text, 0, end text, start text, 1, end text, start text, 0, end text, start text, 0, end text, start text, 10, end text, start text, 0, end text, start text, 0, end text, start text, 11, end text, start text, 0, end text, start text, 10, end text, start text, 0, end text, start text, 0, end text, start text, 101, end text, start text, 0, end text, start text, 110, end text, start text, 0, end text, start text, 111, end text, start text, 1, end text, start text, 0, end text, start text, 0, end text, start text, 0, end text, start text, 1, end text, start text, 0, end text, start text, 0, end text, start text, 1, end text, start text, 1010, end text.## Refresher: Decimal numbers

Before exploring how the binary system works, let's revisit our old friend, the decimal system. When you learned how to count, you might have learned that the right-most digit is the "ones' place", the next is the "tens' place", the next is the "hundreds' place", etc.

Another way to say that is that the digit in the right-most position is multiplied by 1, the digit one place to its left is multiplied by 10, and the digit two places to its left is multiplied by 100.

Let's visualize the number 234:

2 | 3 | 4 |
---|---|---|

hundreds' place | tens' place | ones' place |

100 | 10 | 1 |

When we multiply each digit by its place, we can see that 234 is equal to left parenthesis, 2, times, 100, right parenthesis, plus, left parenthesis, 3, times, 10, right parenthesis, plus, left parenthesis, 4, times, 1, right parenthesis.

We can also think of those places in terms of the powers of ten. The ones' place represents multiplying by 10, start superscript, 0, end superscript, the tens' place represents multiplying by 10, start superscript, 1, end superscript, and the hundreds' place represents multiplying by 10, squared. Each place we add, we're multiplying the digit in that place by the next power of 10.

2 | 3 | 4 |
---|---|---|

hundreds' place | tens' place | ones' place |

100 | 10 | 1 |

10, squared | 10, start superscript, 1, end superscript | 10, start superscript, 0, end superscript |

## Binary numbers

The binary system works the same way as decimal. The only difference is that instead of multiplying the digit by a power of 10, we multiply it by a power of

**2**.Let's look at the decimal number 1, represented in binary as start text, 0, end text, start text, 0, end text, start text, 0, end text, start text, 1, end text:

start text, 0, end text | start text, 0, end text | start text, 0, end text | start text, 1, end text |
---|---|---|---|

8 | 4 | 2 | 1 |

2, cubed | 2, squared | 2, start superscript, 1, end superscript | 2, start superscript, 0, end superscript |

That's the same as left parenthesis, 0, times, 8, right parenthesis, plus, left parenthesis, 0, times, 4, right parenthesis, plus, left parenthesis, 0, times, 2, right parenthesis, plus, left parenthesis, 1, times, 1, right parenthesis, or 0, plus, 0, plus, 0, plus, 1.

Okay, perhaps you could have guessed that one — now for a bigger number!

The decimal number 10 is represented in binary as start text, 1010, end text:

start text, 1, end text | start text, 0, end text | start text, 1, end text | start text, 0, end text |
---|---|---|---|

8 | 4 | 2 | 1 |

2, cubed | 2, squared | 2, start superscript, 1, end superscript | 2, start superscript, 0, end superscript |

That's the same as left parenthesis, 1, times, 8, right parenthesis, plus, left parenthesis, 0, times, 4, right parenthesis, plus, left parenthesis, 1, times, 2, right parenthesis, plus, left parenthesis, 0, times, 1, right parenthesis, or 8, plus, 0, plus, 2, plus, 0. Indeed, binary start text, 1010, end text equals the decimal 10.

If you managed to figure that out, congratulations! If not, that's totally expected: there are techniques that will help you convert between the number systems, and it's

*much*easier when you learn those techniques.### Converting decimal to binary

Here's my favorite way to convert decimal numbers to binary:

- Grab a piece of paper or a whiteboard.
- Draw dashes for each of the bits. If the number is less than 16, draw 4 dashes. Otherwise, for numbers up to 255, draw 8 dashes. Bigger numbers than that require more bits and take a while to do by hand, so let's focus on the smaller numbers.
- Write the powers of 2 under each dash. Start under the right-most dash, writing 1, then keep multiplying by 2.
- Now start at the left-most dash and ask yourself "Is the number greater than or equal to this place value?" If you answer yes, then write a 1 in that dash and subtract that amount from the number. If you answer no, then write a 0 and move to the next dash.
- Keep going from left to right, keeping track of how much remainder you still need to represent. When you're done, you'll have converted the number to binary!

Here's what that looks like for the decimal number 6:

*"Hmm, 6 is less than 16, so 4 bits is plenty..."*

start fraction, divided by, 8, end fraction
start fraction, divided by, 4, end fraction
start fraction, divided by, 2, end fraction
start fraction, divided by, 1, end fraction

*"Well, 6 is less than 8, so I'll write a 0 first..."*

start fraction, 0, divided by, 8, end fraction
start fraction, divided by, 4, end fraction
start fraction, divided by, 2, end fraction
start fraction, divided by, 1, end fraction

*"6*

*is*bigger than 4, so I'll write a 1 next..."start fraction, 0, divided by, 8, end fraction
start fraction, 1, divided by, 4, end fraction
start fraction, divided by, 2, end fraction
start fraction, divided by, 1, end fraction

*"Ok, 6 - 4 = 2, so I still need to represent 2. Let me note that..."*

start fraction, 0, divided by, 8, end fraction
start fraction, 1, divided by, 4, end fraction
start fraction, divided by, 2, end fraction
start fraction, divided by, 1, end fraction
start text, left parenthesis, R, e, m, a, i, n, d, e, r, colon, space, 2, right parenthesis, end text

*"2 is equal to 2, so I'll write a 1 next..."*

start fraction, 0, divided by, 8, end fraction
start fraction, 1, divided by, 4, end fraction
start fraction, 1, divided by, 2, end fraction
start fraction, divided by, 1, end fraction

*"2 - 2 = 0, so there's nothing left to represent!"*

start fraction, 0, divided by, 8, end fraction
start fraction, 1, divided by, 4, end fraction
start fraction, 1, divided by, 2, end fraction
start fraction, divided by, 1, end fraction
start text, left parenthesis, R, e, m, a, i, n, d, e, r, colon, space, 0, right parenthesis, end text

*"I'll fill a 0 in the last bit, since I'm all done now..."*

start fraction, 0, divided by, 8, end fraction
start fraction, 1, divided by, 4, end fraction
start fraction, 1, divided by, 2, end fraction
start fraction, 0, divided by, 1, end fraction

In case you're wondering: there's only one way to represent any given number in binary, just like there's only one way to represent any given number in decimal. Any technique that you use for converting a decimal to binary number should yield the same number.

Try another conversion now, using that technique or your own.

### Patterns in binary numbers

In those last two questions, you converted odd numbers. There's something interesting about odd numbers in binary. Here are a few more odd numbers to give you an idea:

Decimal | Binary |
---|---|

3 | start text, 0, end text, start text, 0, end text, start text, 11, end text |

5 | start text, 0, end text, start text, 101, end text |

7 | start text, 0, end text, start text, 111, end text |

9 | start text, 10, end text, start text, 01, end text |

Do you see the pattern?

You don't actually need to convert those large numbers to decimal to answer the question—you only need to look at a single bit of information—the very last bit. The last bit is always the ones' place, and if a number is odd, it must have a 1 in that ones' place. There's no way to create an odd number in the binary system without that ones' place, since every other place is a power of 2. Knowing this can give you a better intuitive understanding of binary numbers.

There's another interesting pattern in binary numbers. Take a look at these:

Decimal | Binary |
---|---|

3 | start text, 11, end text |

7 | start text, 111, end text |

15 | start text, 1111, end text |

Each of the decimal numbers are a power of 2, minus 1: 4, minus, 1, equals, 3, 8, minus, 1, equals, 7, 16, minus, 1, equals, 15. When a binary number has a 1 in each of its places, then it will always equal the largest number that can be represented by that number of bits. If you want to add 1 to that number, you need to add another bit. It's like 9, 99, and 999 in the decimal system.

As it turns out, the highest number that can be represented by n bits is the same as 2, start superscript, n, end superscript, minus, 1:

Bits (n) | Highest number | (2, start superscript, n, end superscript, minus, 1) |
---|---|---|

1 | 1 | left parenthesis, 2, start superscript, 1, end superscript, minus, 1, right parenthesis |

2 | 3 | left parenthesis, 2, squared, minus, 1, right parenthesis |

3 | 7 | left parenthesis, 2, cubed, minus, 1, right parenthesis |

4 | 15 | left parenthesis, 2, start superscript, 4, end superscript, minus, 1, right parenthesis |

You could calculate that using our strategy from before fairly quickly. However, there's one more strategy, keeping in mind what we just learned: you could count the number of bits (5), calculate 2, start superscript, 5, end superscript as 2, times, 2, times, 2, times, 2, times, 2, equals, 32, and then subtract 1.

All of this is to help you gain a more intuitive understanding of binary. You may not remember all of this, and that's okay. There's lots of practice coming up for you to build your skills.

🙋🏽🙋🏻♀️🙋🏿♂️Do you have any questions about this topic? We'd love to answer— just ask in the questions area below!

## Want to join the conversation?

- I'm still confused with conversions and the process of converting in general. If you have the number "7" I don't understand how you could convert that. Here's my thinking:
`7 is less than 16, so we just need 8421 as the digits.`

8421

If each value is greater than 7, 1 to convert it.

If each value is less than 7, have a 0 to convert it.

Can I just get some help and clarification on this? Thanks in advance for answers!**Edit**: See Tips and Thanks I posted on this page, as well as this link to help you understand:

https://www.khanacademy.org/math/algebra-home/alg-intro-to-algebra/algebra-alternate-number-bases/v/decimal-to-binary(9 votes)- The key is to start from the left side and go to the right-- and not consider the right digits until we've taken care of the leftmost.

Put another way:

Starting from the left, we're trying to "fill up" each digit when possible, and we only go to the next digit when we have leftover value to represent.

So for 7:

- We can't put a 1 in the 8 place, because 8 is greater than 7. Therefore, we have to put a 0 and move to the right.

- We**can**put a 1 in the 4 place, because 4 is less than 7. So we put a 1 and move to the right. Our number only represents 4 so far, so there's 3 leftover.

- We**can**put a 1 in the 2 place, because 2 is less than 3. So we put a 1 and move to the right. Now our number represents 6, so there's 1 leftover.

- Fortunately, we're now in the 1 place, so we can put a 1 in it. We've now represented the number 7.

Does that explanation help, or is it still fuzzy?(39 votes)

- I understand binary pretty well. However, in the computer programming section, we sometimes use hexadecimals(sixteen base) in coloring. Why would we use hexadecimal input, if binary would work better?(4 votes)
- Although computers use binary, hexadecimal is more compact, and easier for humans to read and understand. That makes things that use hex (like colors) simpler.(13 votes)

- I'm still stuck with "How would you represent the decimal number 25 in binary?" so for 32 i put 0 , for 16 i put 1, for 8 is 1, for 4 it is 1, for 2 it is 1, and for one it is 1. Overall , i got 011111, but it is wrong.(4 votes)
- 16+8+4+2+1 = 31 not 25

25 < 32 so the 6th digit is 0

25 > 16 so the 5th digit is 1 and 25 - 16 = 9

9 > 8 so the 4th digit is 1 and 9 - 8 = 1

1 < 4 so the 3rd digit is 0

1 < 2 so the 2nd digit is 0

1 = 1 so the 1st digit is 1 and 1 - 1 = 0

Altogether, we have 011001(6 votes)

- Sorry but I don't understand the last question how do you know that 1111 represent 31 in decimal? Someone please explain this to me.(2 votes)
- Actually, 1111 represents 15, not 31. You can tell by using this equation: (1*8)+(1*4)+(1*2)+(1*1)=15. 31 is represented by 11111.(3 votes)

- how do i know binary 1010 is equals to the decimal 10.(1 vote)
**Binary**is based on a base of 2. For example, 1010 could also be written as 2^0(0)+2^1(1)+2^2(0)+2^3(1)=10. It can also be written as 1(0)+2(1)+4(0)+8(1)=10. In the decimal number system, each place value is multiplied from right to left 10 times, or if going from left to right, divided by ten. For example, the number 1560 could also be written as**1**(1000)+**5**(100)+**6**(10)+**0**(1). You could also think of this number as 10^3(**1**)+10^2(**5**)+10^1(**6**)+10^1(**0**). Does this look familiar? This is the same way it works for the binary number system. Except in the decimal number system, it is 1, 10, 100, 1000, 10000, and so on. And in the binary number system, it is 1, 2, 4, 8, 16, and so on.(4 votes)

- 1. So there are different binary standards?

2. Is this why computers come with some hard drive space used?(2 votes)- 1. Yes, there are different standards for how numbers should be stored in binary. One of the most used today is the IEEE 754 standard.

2. Some of the computer memory is used up by firmware such as the BIOS. More memory is also used to store the operating system. (Windows 10 OS takes up about 60 gigabytes.)(2 votes)

- How would one represent a decimal such as .75 or .25 in binary? And is it possible for there to be decimals in binary(like 1011.01)?(1 vote)
- From the author:Great question! We actually discuss representing decimals in the next lesson about number limits - take a look at that article and come back if you'd like additional clarification (or post there).

https://www.khanacademy.org/computing/ap-computer-science-principles/x2d2f703b37b450a3:digital-information/x2d2f703b37b450a3:number-limits-overflow-and-roundoff/a/number-limits-overflow-and-roundoff(4 votes)

- Why do we have to add a zero to the left of a binary number to make four bits instead of three.

Example : 0110 = 6; instead of 110 = 6(2 votes)- You do not technically need to add the extra 0, but you do if you are using 4 bits to store your data. In this case, we are making that assumption.

We can also make the assumption that we are representing numbers with 32-bits, which is the case when you are using an integer data type in many conventional programming languages. With 32-bits, 6 would be represented as 00000000000000000000000000000110.

It comes down to how many bits you want to use when representing your numbers in binary.(2 votes)

- any binary number dat ends with one is an odd binary number, no matter how long the binary number is(2 votes)
- Yes,if you want to represent an odd number in binary, you must end with ones place, since the other place is a power of 2.(1 vote)

- where it says: You might be wondering why the possible answers there used 8 bits, even though none of them had a 1 in the left 3 bits. We often write bits in groups of 4 because of how computers process them.

I do not understand how they are grouping the bits though(2 votes)