If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

## Computers and the Internet

### Course: Computers and the Internet>Unit 1

Lesson 2: Binary numbers

# Binary numbers

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:
234
hundreds' placetens' placeones' place
100101
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.
234
hundreds' placetens' placeones' place
100101
10, squared10, start superscript, 1, end superscript10, 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 textstart text, 0, end textstart text, 0, end textstart text, 1, end text
8421
2, cubed2, squared2, start superscript, 1, end superscript2, 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 textstart text, 0, end textstart text, 1, end textstart text, 0, end text
8421
2, cubed2, squared2, start superscript, 1, end superscript2, 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.
Now you try it: How would you represent the decimal number 6 in binary?

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:
1. Grab a piece of paper or a whiteboard.
2. 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.
3. Write the powers of 2 under each dash. Start under the right-most dash, writing 1, then keep multiplying by 2.
4. 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.
5. 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.
How would you represent the decimal number 11 in binary?

Let's go bigger. How would you represent the decimal number 25 in binary?

### 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:
DecimalBinary
3start text, 0, end text, start text, 0, end text, start text, 11, end text
5start text, 0, end text, start text, 101, end text
7start text, 0, end text, start text, 111, end text
9start text, 10, end text, start text, 01, end text
Do you see the pattern?
If you think you figured it out, try this question: which of the following very large binary numbers is odd?

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:
DecimalBinary
3start text, 11, end text
7start text, 111, end text
15start 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)
11left parenthesis, 2, start superscript, 1, end superscript, minus, 1, right parenthesis
23left parenthesis, 2, squared, minus, 1, right parenthesis
37left parenthesis, 2, cubed, minus, 1, right parenthesis
415left parenthesis, 2, start superscript, 4, end superscript, minus, 1, right parenthesis
What do you think: what does start text, 11111, end text represent in decimal?