While humans are accustomed to working with decimal numbers, or numbers in the base 10 system, computers work with binary numbers, or numbers in the base 2 system. Being able to work with binary numbers is a requirement in almost every field of computer science and cybersecurity and a basic skill you’ll need if you want to earn your Junior Geek badge. In this page you’ll the basics of counting in binary, how to convert from binary to decimal, as well as how to count in hexadecimal and a trick for easily converting between binary and hex.
Review of Number Systems and Counting in Decimal
Before we jump into binary, we’ll do a quick review of base 10 or the decimal number system you’re already familiar with to provide you with a perspective.
In base 10 the places in a number are assigned values. That is, the right most numeral is in what is referred to as the 1’s place. Moving left, the next numeral is in what’s referred to as the 10’s place, followed by the 100’s place, followed by the 1000’s place, etc. Each place value is calculated by raising the number base, in this case 10, to a power starting with 0 on the right and increasing the power by 1 for each position as you move left. That is, the value of the first place is 100, the value of the second place (moving left) is 101, the value of the third place is 102, and the value of the fourth place is 103.
Remember that any number to the 0th power is 1, and any number to the 1st power is itself.
| _______________ | _______________ | _______________ | _______________ |
| 103 | 102 | 101 | 100 |
| 1000’s place | 100’s place | 10’s place | 1’s place |
Calculating the total value of a number is done by multiplying the numeral in each place by the place value, and then summing the result of all the multiplications. For example, if we had the number 4378 the total value would be:
| ______4_______ | _______3_______ | _______7_______ | _______8_______ |
| 103 | 102 | 101 | 100 |
| 1000’s place | 100’s place | 10’s place | 1’s place |
(4×1000) + (3×100) + (7×10) + (8×1)
The point of this isn’t to tell you what 4378 is. I know you know what it is, even without calculating (4×1000) + (3×100) + (7×10) + (8×1). We can all pretty much look at numbers in base 10 and tell exactly what they are. The point of this to remind you how number systems work, as binary or base 2 works exactly the same way with a few differences.
Binary or Base 2
In binary or base 2 there are only two numerals, 0 and 1. This makes binary very useful when working with electronic devices such as computers as 0 can be used to represent no voltage or a switch that’s turned off, and 1 can be used to represent a voltage or a switch that’s turned on.
The process for counting in binary is best demonstrated by repeating the process we used for figuring out the place values for decimal numbers, except in this case each place is going to be a power of 2 instead of a power of 10. The following diagram shows the values of each place in a 4-digit binary number. Once again remember that any number to the 0th power is 1 and number to the first power is itself. After that you can find the value of each place by multiplying the value to the right by 2.
| _______________ | _______________ | _______________ | _______________ |
| 23 | 22 | 21 | 20 |
| 8’s place | 4’s place | 2’s place | 1’s place |
Counting in binary is pretty simple, it just gets cumbersome quickly since there are only two numerals. The first number is 0 followed by, just like in decimal. When we get to 2 the binary equivalent is 102. This is because we’ve run out of numerals, and just like in decimal when we hit 9, the next thing to do is to add a 1 to the next place and set the values in all the other places to 0. In decimal adding 1 to 9 gives 10, or adding 1 to 999 gives 1000. In binary adding 1 to 1 results in 10 and adding 1 to 111 results in 1000. The following shows the decimal numbers from 0 to 15 and their binary equivalents.
| Decimal | Binary |
|---|---|
| 0 | 0 0 0 0 |
| 1 | 0 0 0 1 |
| 2 | 0 0 1 0 |
| 3 | 0 0 1 1 |
| 4 | 0 1 0 0 |
| 5 | 0 1 0 1 |
| 6 | 0 1 1 0 |
| 7 | 0 1 1 1 |
| 8 | 1 0 0 0 |
| 9 | 1 0 0 1 |
| 10 | 1 0 1 0 |
| 11 | 1 0 1 1 |
| 12 | 1 1 0 0 |
| 13 | 1 1 0 1 |
| 14 | 1 1 1 0 |
| 15 | 1 1 1 1 |
Converting between binary and decimal is also done using the same general process used for decimal numbers. That is, first multiply the number in each place by its place value, and then sum the results of all the multiplications. For example, 01102 would be (0x8) + (1×4) + (1×2) + (0x1) = 610.
| _______0_______ | _______1_______ | _______1_______ | _______0_______ |
| 23 | 22 | 21 | 20 |
| 8’s place | 4’s place | 2’s place | 1’s place |
Most uses of binary numbers, such as IP addresses contain 8 bits in each place. This means you’ll need to have 8 places, with the left most place having the value of 27 or 128 and the right most having the value 20 or 1. The following shows all the place values for an 8-bit number.
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 |
