A number can be written in any (integer, or whole-number) base greater than 1. A base is a reference to the legal digits that any given expressed number may contain. Generally speaking, if n were used to denote the base that the number is written in, then the legal digits that the number may contain are 0 - (n-1). So if a number were written in base 7, it could only contain digits ranging from 0 to 6. The base is typically written as a subscript following the number; if the base is omitted then the generally implied base is 10.
The Decimal System
The decimal system, also known as base-10, is the number system that you were likely first introduced to at a very young age. It is in this system that you are also probably most comfortable interpreting numbers in, and so we will start by exploring decimal numbers, and we will then move on to explore conversions from other bases to base-10, and from base-10 to other bases.Because the decimal system is actually just another name for base-10, the legal values for a digit in the decimal system are 0 - (10-1), or 0-9. When the number exceeds 9, you add another digit; the integer directly following 9 on the number line is 10. But what does that additional '1' digit mean? After 99, you get 100. Why another '1', and two '0' digits? Here's the simple explanation:
In any given expressed number, each digit is given an implied label counting its offset from the first digit in front of the decimal point; the left-most digit has the largest offset, and the right-most digit has the lowest offset. So, for the number 123, we have the following setup:
Label | 2 | 1 | 0 |
---|---|---|---|
Digit | 1 | 2 | 3 |
Now, what is the relevance of this label? This label is used to compute the implied value of a digit. To get the overall value of a number, you sum the implied value of all digits. Again, using n as the base, using d as the digit value, and x as the label value, the implied value of any given digit is: d * nx. The following is the calculation table for the base-10 number 123:
Digit | Expression | Value |
---|---|---|
1 | 1 * 102 | 100 |
2 | 2 * 101 | 20 |
3 | 3 * 100 | 3 |
123 | 100 + 20 + 3 | 123 |
Of course, converting the number 123 to the number 123 is not at all useful, but this knowledge should hopefully give you a better understanding of numbers in general, which can help us move on to other common number systems.
The Binary System
The binary system, also known as base-2, is the language of computers. Being base-2, it is entirely based on 1s and 0s. As a programmer interacting with a computer, binary knowledge is one of the most useful things you can attain. The rules are simple, of course; they are the exact same as with the decimal system. As an example, get the sum of the implied values of the following number: 11012Label | 3 | 2 | 1 | 0 |
---|---|---|---|---|
Digit | 1 | 1 | 0 | 1 |
Digit | Expression | Value |
---|---|---|
1 | 1 * 23 | 8 |
1 | 1 * 22 | 4 |
0 | 0 * 21 | 0 |
1 | 1 * 20 | 1 |
11012 | 8 + 4 + 0 + 1 | 13 |
The Hexadecimal System
The hexadecimal system, also known as base-16, is another common representation in programming. Being base-16, it is based on digits ranging from 0 to 15. However, the numbers 10-15 require multiple digits to represent, and so to represent them with a single digit we turn to the alphabet. The following table shows the relationships between certain alphabet characters and the numbers:10 | 11 | 12 | 13 | 14 | 15 |
A | B | C | D | E | F |
Again, following the same rules as before, we'll convert the number 2AF16:
Label | 2 | 1 | 0 |
---|---|---|---|
Digit | 2 | A | F |
Digit | Expression | Value |
---|---|---|
2 | 2 * 162 | 512 |
A | 10 * 161 | 160 |
F | 15 * 160 | 15 |
2AF16 | 512 + 160 + 15 | 687 |
Converting to Other Bases
Great, now we can convert from other bases to base-10. This is useful for interpreting information, but how do we convert from base-10 to another base? Throughout this section, we will use the working number 13.One method of converting to another base from base-10 is to find the largest power of the target base that is smaller than the number to be converted. So to convert to binary, find the largest power of 2 that is smaller than 13:
20=1 | 21=2 | 22=4 | 23=8 | 24=16 |
Since the exponent 3 is the smallest that will fit into the number, we know that offset 3 is the location of the first digit, meaning the number will have 4 digits. To determine the value of this first digit, we find how many times 23 (8) goes into the target number (13). It goes in one time, so the current working number is 1nnn. Now subtract 8 from 13, leaving 5. For each decreasing exponent, we repeat the process (how many times does 2x go into the current working number? Subtract that value. So, in sequence:
1. How many times does 22 (4) go into 5? 1. Working number: 11nn. Subtract 4 (1*22) from 5.
2. How many times does 21 (2) go into 1? 0. Working number: 110n. Subtract 0 (0*21) from 1.
3. How many times does 20 (1) go into 1? 1. Final number: 1101. 11012 = 13
Now apply the same method to convert 13 to hexadecimal: Smallest power of 16 that goes into 13? 0. How many times does 160 (1) go into 13? 13. Hexadecimal digit corresponding to 13? D. 13 = D16.
Converting between hexadecimal and binary is also a simple task. It turns out that every digit in hexadecimal (base-16) corresponds to 4 digits in binary (base-2). 24 = 16, so the relationship is very clear. For example, the single hexadecimal digit F (15 in base-10) corresponds to the binary value 1111. Since computers 'think' in binary, and binary numbers are relatively lengthy, it is often useful to simplify numbers into their hexadecimal values; using hexadecimal instead of binary results in a significant reduction in size, and the conversion between the two is a simple process.
Two's Complement For Negative Numbers
Computers need a way to represent negative numbers. Signed numbers (numbers that may be negative) use the high-order bit (that is, the left-most digit in the binary number) as a sign bit; if the bit is 0, the number is positive. If it is 1, the number is negative. In addition, if the number is negative, the following bits are interpreted differently. In the following examples, we will use 4-bit binary numbers for simplicity.When interpreting binary numbers as signed (+/-) integers, the first bit is used as a sign bit, and the following bits indicate the number. If the number is positive (the sign bit is 0), then the number is calculated normally: 00012=1, 00112=3, etc. If the number is negative, however, the value of the bits following the sign bit is subtracted from 2n, where n is the number of bits in the total number minus 1. For example, the number 10112:
The sign bit is 1, so the number is negative. The value of the remaining bits is 3. You subtract 3 from 2n, and since there are 4 bits in the number, n is (4-1), or 3. 23 is 8, so by subtracting 3 from 8 we find that this number is -5. As you can see, the highest 4-bit negative number is achieved by 11112, which is -1 (subtract 7 from 8). The lowest 4-bit negative number is 10002, which is -8 (subtract 0 from 8). The highest positive number is 01112, or 7, meaning that with a 4-bit signed number we have a range of -8 to 7.
You might be wondering why this seemingly silly method of calculating numbers is used; why not just use the sign bit and calculate the remaining bits normally? Two's complement notation allows us to maximize the range of values any one number can achieve by eliminating the concept of two zeros (positive and negative zero). As an example, if we simply calculated the bits following the sign bit normally, we would have positive zero with 00002 and negative zero with 10002.
It follows that two's complement also allows subtraction using standard addition circuits, by putting one of the numbers into it's two's complement format.