📺 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
, , , , , , , , , .
As we just learned, computers represent all information in bits. In order to represent numbers with just
s and s, computers use the binary number system. Here's what it looks like when a computer counts to ten: , , , , , , , , , .
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
, the digit one place to its left is multiplied by , and the digit two places to its left is multiplied by .
Let's visualize the number
|hundreds' place||tens' place||ones' place|
When we multiply each digit by its place, we can see that
is equal to .
We can also think of those places in terms of the powers of ten. The ones' place represents multiplying by
, the tens' place represents multiplying by , and the hundreds' place represents multiplying by . Each place we add, we're multiplying the digit in that place by the next power of .
|hundreds' place||tens' place||ones' place|
The binary system works the same way as decimal. The only difference is that instead of multiplying the digit by a power of
, we multiply it by a power of .
Let's look at the decimal number
, represented in binary as :
That's the same as
, or .
Okay, perhaps you could have guessed that one — now for a bigger number!
The decimal number
is represented in binary as :
That's the same as
, or . Indeed, binary equals the decimal .
Now you try it: How would you represent the decimal number
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
, draw dashes. Otherwise, for numbers up to , draw 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
under each dash. Start under the right-most dash, writing , then keep multiplying by .
- 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
in that dash and subtract that amount from the number. If you answer no, then write a 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
"Hmm, 6 is less than 16, so 4 bits is plenty..."
"Well, 6 is less than 8, so I'll write a 0 first..."
"6 is bigger than 4, so I'll write a 1 next..."
"Ok, 6 - 4 = 2, so I still need to represent 2. Let me note that..."
"2 is equal to 2, so I'll write a 1 next..."
"2 - 2 = 0, so there's nothing left to represent!"
"I'll fill a 0 in the last bit, since I'm all done now..."
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
Let's go bigger. How would you represent the decimal number
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:
Do you see the pattern?
Check your understanding
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
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 . 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:
Each of the decimal numbers are a power of
, minus : , , . When a binary number has a 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 to that number, you need to add another bit. It's like , , and in the decimal system.
As it turns out, the highest number that can be represented by
bits is the same as :
|Bits (||Highest number||(|
What do you think: what does
represent in decimal?
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 (
), calculate as , and then subtract .
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.
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.
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:
- 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?(49 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.(15 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.(6 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(8 votes)
- The difficulty gap between the previous chapter and this one is astounding.(5 votes)
- I know know you do base 10 and base 2 but what about base 16(4 votes)
- That's pretty neat. I wonder how that relates to another pattern of odd numbers when you add them:
1+3 = 4,
1+3+5 = 9,
1+3+5+7 = 16, and skipping ahead a bit,
1+3+5+7+9+11+13 = 49.
And no matter how far you keep going, you will always land on a perfect square. Plus, if you look closer, you might notice another pattern. The sum of the first three odd numbers = 9 = 3², add the next one to get 4², and so on.
This is why x² + 2x + 1 always lands on a square. It is a way to add the next odd number to a known square(x * x);
Forgive me, I just find that very satisfying for some reason.(5 votes)
- You've decided to use binary to communicate "secret" messages to your friend, and you want to tell them to meet you at locker 44.
What would be the binary representation of the decimal number 44?(3 votes)
- good question! since we have 44 as our number, we count our base-2 bits now(2^0 all the way up to 2^5). So 2^5 is 34, which is less than 44 meaning we should keep a 1 in that position. then we have 2^4, which is 16, and 32+16 is greater than 44, so we don't want 16 thus keep a 0 in the 2^4 place. Then we move down to 2^3, which is 8, and we do 32+8, (32 because we already established we would include 32 in our binary rep because its smaller than 44) which would give us 40. Since that's still less than 44, we keep a 1 in the 2^3 place and move further down to 2^2. We then take 2^2, which is 4, and add 40 + 4, which gives us exactly 44! Since we got our answer, we fill the 2^2 place with 1, and fill the rest of the spots (2^1, 2^0) with 0 because we don't need them anymore. Thus, our answer would be 101100. I hope that helped!(5 votes)
- The pattern of the values of the bits appears to look like the Fibonacci sequence, is that right?(3 votes)
- huh? do you know what the fibonacci sequence is? it begins with : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89. I don't see any relationship with binary here.(4 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.(3 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)
- I’m confused about how to convert 25 into binary number. I tried to solve it by myself, but unfortunately I got wrong. Here is what I thought: I first drew 8 dashes. Than I started form the very left: From 128 to 32, 25 is always less than these three numbers. At 16, because 25 is greater than 16, so I wrote “1”. 25 subtract 16 equals to 9. 9 is greater than 8, so I wrote “1”. 9 subtract 8 equals to 1. 1 is less than 4, so I wrote “0”. After that I moved to the digit “2”. 25 is greater than 2, so I wrote “1”. 25 subtract 2 equals to 23. 23 is greater than 1, so I wrote “1”. Finally, I got my answer: 25 converted to binary number is 00011011. However, this is incorrect. It should be 00011001. Can someone explain this to me? Many thanks.(3 votes)
- Hi. You have the method down already I suggest you just pay more attention to how you solve. You were correct until you said 25 is greater than 2 so you wrote 1. While 25 is greater than 2, recall you already had 1 under the 16 and 8 meaning 16+8 = 24
25 - 24 = 1 1 is less than 2 so where you wrote '1' for 2 you were meant to write '0' then where you had 1 you should have written '1'. It's alright these things happen it's just a matter of paying attention to detail.(3 votes)