By now you are familiar with the fact that computer CPUs, memory, and hard drives deal with code and data represented in binary, which are sequences of 1s and 0s. In order for computers to accomplish useful things with this model of operation, computer scientists needed to develop ways of representing various kinds of data that are easily understandable by humans (such as decimal numbers, text, and images) using this limited representation. In this section, we will address how computers represent numeric values, and in the next we’ll take the stop to seeing how this then can translate to text. We eventually will get to talking about the image format PPM, but that is for a much later lesson (if you just can’t wait, feel free to skip down to the “PPM” lessons later on).
Most people are used to thinking about numeric values using the decimal number system. We say and think things like “This pizza costs 29 dollars” or “I have 7 bicycles.” However, the decimal number system is only one of many numeric systems that we can use to represent quantities of money, items, etc. The “dec” in decimal comes from the Greek word “deka” meaning “ten.” This means that there are ten unique digits we can use for representing numbers (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) We can combine one or more of these digits to act as a representation for a quantity of something we want to explain in the real-world. For small values, we can get away with using a single digit (“I have 7 bicycles”). Once we need to represent more than 9 items, we then need to add a second digit (“This pizza costs 29 dollars”). This takes us up to 99, after which we again need to add a digit to continue to represent larger values (“My studio apartment is 503 square feet”). Of course, this pattern continues for all numbers as they get larger and larger.
Binary is just another number system like decimal numbers. The “Bi” in binary means “two.” Thus, instead of having ten digits to work with before needing to add an additional one, we only have two. The principles of how we count, how numbers grow, and how math is done follows the same exact patterns as decimal numbers, but will likely seem odd at first, at least to those who have never used binary before. I’ll list out the first ten decimal numbers with the corresponding binary below - can you see the pattern?
Notice how the binary numbers change.
After using 0
and 1
, we have already exhausted all of the possible 1-digit numbers.
Thus, in order to represent higher values, we need to jump to two digits to represent the decimal values 2 and 3 (10
and 11
).
We’ve only reached 3, but yet again we are at an exhaustion point.
The only way to represent higher values is to jump to three digits, 100
for 4, 101
for 5, and so on.
1101
1100
1010
1110
1011
10
11
12
13
14
Operations such as addition and subtractions again function the same, but with less digits to work with. Imagine you were to add the decimal numbers 15 and 87 to get 102. When adding these, you would add the first two digits (5+7 = 12). At this point, you’d know the resulting number will have 2 as the least-significant digit, and the 1 would carry. We then add 1+1+8 = 10. Thus, the final result is 102.
In binary addition is the same set of steps.
When adding 1011
(eleven) and 1110
(fourteen) we would do these steps:
XXXX1
XXX01
with a 1 carried.XX001
with a 1 carried.X1001
with a 1 carried11001
Humans think in decimal and computers “think” in binary. Due to this, a useful skill for programmers to develop is the ability to quickly convert between decimal and binary numbers. One approach to this would be to count from zero up to N whenever you need to do the conversion to some value N. This would work well for small numbers (say, less than 20 or 30), but after that this would start to become very unruly. What would you do if you need to convert the “503 square feet” to binary? That’s a lot of counting. A better procedure for completing these conversions is presented in the next lesson.