As we saw in an earlier post, a CPU only listens in binary. Of the vast infinity of real numbers, it can only make out 0 or 1. (Of course, we could have called these 2 and 5, 1500000829 and 469, or whatever other pair. The point is that there are only two elements to things: Ying-Yang, Good-Evil, Light-Dark, etc.) All data is stored in a computer’s memory as binary digits.
A bit is one binary digit:
A byte consists of eight slots holding eight binary digits:
Since only two different kinds of values can go in each slot, we call this base 2 system. By contrast, most people are accustomed to the decimal (base 10) system, in which every individual slot can hold one of the values from 0 through 9.
There are other number systems, such as the octal (base 8) system, which uses the digits from 0 to 7, and the hexadecimal (base 16) system, which uses the digits 0 to 9 and the letters A to F.
The hexadecimal uses letters instead of going from 0 to 15 because encoding the idea of 15 or 12 or 13 using two symbols would go against the correspondence of one symbol per slot.
So lets analyze the funniest three digit number in the decimal system, one that is a Sophie Germain prime, the international calling code for Bulgaria, the year of Barbatio’s death, yup, you guessed it: 359.
3 exists in the hundreds slot (10² slot)
5 exists in the tens slot (10¹ slot)
9 exists in the ones slot (10⁰ slot)
Therefore, we can write 359 as
359 = 3*10² + 5*10¹ + 9*10⁰
Thus, the decimal number 359 is written as a linear combination of powers of 10 with coefficients from the base 10 alphabet, that is, the digits from 0 to 9. Similarly, the binary number 11011 is written as a linear combination of powers of 2 with coefficients from the base 2 alphabet, that is the digits 0 and 1.
For example, the binary number 11011 can be written as
11011 = 1*2⁴ + 1*2³ + 0*2² + 1*2¹ + 1*2⁰
Here is a list of the binary equivalents for the decimal numbers 0 through 8
And here is a list of the powers of 2 and their decimal equivalents
As we count in increments of 1, the last digit alternates between 0 and 1. In fact, we can see that for even numbers, the last digit is always 0 and for odd numbers, the last digit is always 1.
Because computers store numbers as binary, and people recognize numbers as decimal values, conversion between the decimal and binary number systems often takes place inside a computer.
Let’s try a few conversions. To convert a binary number to a decimal number, multiply each digit in the binary number by 2ᵖᵒˢᶦᵗᶦᵒⁿ⁻¹, counting the rightmost position as position 1 and moving left through the binary number. Then add the products together.
Using this method, we can calculate the equivalent of the binary number 11010 in our decimal system.
11010 = 1*2⁴ + 1*2³ + 0*2² + 1*2¹ + 0*2⁰
= 16 + 8 + 0 + 2 + 0
Now let’s examine how to convert a decimal number to a binary number. Let’s convert the year Shapur II halted his campaign (the decimal number 359) into its binary number equivalent. As we can see from the way we rewrote 11011, a binary number can be written as a sum of powers of 2 with coefficients 0 and 1.
The strategy to decompose a decimal number into a sum of powers of 2 is simple: first find the largest power of 2 that is smaller than or equal to the decimal number, then do the same with the remainder, and so on, until you reach 0.
The largest power of 2 that is smaller than 359 is 256, or 28 (the next larger power of 2 would be 512, which is larger than 359). Subtracting 256 from 359 gives us 103 (359 − 256 = 103), so we now have
Now we apply the same procedure to 103. The largest power of 2 that is smaller than 103 is 64, or 26. That means that there is no factor for 27, so that digit’s value is 0. Subtracting 64 from 103 gives us 39.
Now we have
Repeating the procedure for 39, we find that the largest power of 2 smaller than 39 is 32 or 25. Subtracting 32 from 39 gives us 7.
So we now have
Repeating the procedure for 7, the largest power of 2 smaller than 7 is 22, or 4. That means that there are no factors for 24 or 23, so the value for each of those digits is 0. Subtracting 4 from 7 gives us 3, so we have
Repeating the procedure for 3, the largest power of 2 smaller than 3 is 2, or 2¹, and we have:
1 is a power of 2; it is 20, so we finally have
Removing the power of 2 multipliers, 359 can be represented in the binary system as
In a computer program, we will use both positive and negative numbers. Even negative numbers, such as −34, are represented in the binary system. In a computer program, we also use floating-point numbers, such as 3.75. Floating-point numbers are represented using the binary system.