High-level languages shield programmers from the pain of dealing with low-level numeric representation. Writing great code, however, requires a complete understanding of how computers represent numbers . Once you understand internal numeric representation, you'll discover efficient ways to implement many algorithms and see the pitfalls associated with many common programming practices. Therefore, this chapter looks at numeric representation to ensure you completely understand what your computer languages and systems are doing with your data.
Having taught assembly language programming for many years , I've discovered that most people don't understand the fundamental difference between a number and the representation of that number. Most of the time this confusion is harmless. However, many algorithms depend upon the internal and external representations we use for numbers to operate correctly and efficiently . If you do not understand the difference between the abstract concept of a number and the representation of that number, you'll have trouble understanding, using, or creating such algorithms. Fully understanding this difference could take you from creating some mediocre code to creating great code.
A number is an intangible, abstract, concept. It is an intellectual device that we use to denote quantity. Let's say I were to tell you that 'some book has one hundred pages.' You could touch the pages - they are tangible . You could even count those pages to verify that there are one hundred of them. However, 'one hundred' is simply an abstraction that I would be applying to the book as a way of describing its size .
The important thing to realize is that the following is not one hundred:
100
This is nothing more than ink on paper forming certain lines and curves. You might recognize this sequence of symbols as a representation of one hundred, but this is not the actual value 100. It's just three symbols appearing on this page. It isn't even the only representation for one hundred - consider the following, which are all different representations of the value one hundred:
100 | decimal representation |
C | Roman numeral representation |
64 16 | base 16/hexadecimal representation |
1100100 2 | base two/binary representation |
144 8 | base eight/octal representation |
one hundred | English representation |
The representation of a number is ( generally ) some sequence of symbols. For example, the common representation of the value one hundred, '100,' is really a sequence of three numeric digits: the digit 1 followed by the digit followed by a second digit. Each of these digits has some specific meaning, but we could have just as easily used the sequence '64' to represent the value one hundred. Even the individual digits that comprise this representation of 100 are not numbers. They are numeric digits, tools we use to represent numbers, but they are not numbers themselves .
Now you may be wondering why we should even care whether a sequence of symbols like '100' is the actual value one hundred or just the representation of this value. The reason for this distinction is that you'll encounter several different sequences of symbols in a computer program that look like numbers (meaning that they look like '100'), and you don't want to confuse them with actual numeric values. Conversely, there are many different representations for the value one hundred that a computer could use, and it's important for you to realize that they are equivalent.