| Complements in Radix r | Euclid's Division Algorithm | Base Conversion | Java Primitive Data Types: | Integers | Reals | Homework |
Some familiar number systems are radix 10 (decimal), radix 16 (hexadecimal), radix 8 (octal), and radix 2 (binary). Numbers are represented by a string of digit symbols in a given radix. Given a radix r, there are r different symbols available. For a string of digits of length n, there are rn different numbers that can be represented. The following table shows various representations of the first 16 numbers:
| Decimal | Hexadecimal | Octal | Binary | signed decimal based on 4 digit 2's complement |
| 0 | 0 | 0 | 0000 | 0 |
| 1 | 1 | 1 | 0001 | 1 |
| 2 | 2 | 2 | 0010 | 2 |
| 3 | 3 | 3 | 0011 | 3 |
| 4 | 4 | 4 | 0100 | 4 |
| 5 | 5 | 5 | 0101 | 5 |
| 6 | 6 | 6 | 0110 | 6 |
| 7 | 7 | 7 | 0111 | 7 |
| 8 | 8 | 10 | 1000 | -8 |
| 9 | 9 | 11 | 1001 | -7 |
| 10 | A | 12 | 1010 | -6 |
| 11 | B | 13 | 1011 | -5 |
| 12 | C | 14 | 1100 | -4 |
| 13 | D | 15 | 1101 | -3 |
| 14 | E | 16 | 1110 | -2 |
| 15 | F | 17 | 1111 | -1 |
We can easily convert from binary to octal and hexadecimal using three and four digit lookups. Consider the binary number 1010 1111 0111 0010. A three digit lookup gives us the octal representation:
|
1 |
2 | 7 | 5 | 6 | 2. | octal |
| 001 | 010 | 111 | 101 | 110 | 010. | binary |
A four digit lookup gives us the hexadecimal representation:
| A | F | 7 | 2. | hexadecimal |
| 1010 | 1111 | 0111 | 0010 | binary |
We can check our answers by converting each to decimal:
A*163 + F*162 + 7*161 + 2*160 = 44914
1*85 + 2*84 + 7*83 + 5*82 + 6*81 + 2*80 = 44914
215 + 213 + 211 + 210 + 29 + 28 + 26 + 25 + 24 + 21 = 44914
To convert from decimal to a given base, use repeated integer division by the base of conversion recording the remainder each step until the quotient is 0 and you have the final remainder. The first remainder out is adjacent to the radix point (the low order digit).
| Working from bottom to top |
quotients | remainders | quotients | remainders | ||
| 16 | | 0 | 10 = A | 8 | | 0 | 1 | |
| 16 | | 10 | 15 = F | 8 | | 1 | 2 | |
| 16 | | 175 | 7 | 8 | | 10 | 7 | |
| 16 | | 2807 | 2. | 8 | | 87 | 5 | |
| 16 | | 44914 | 8 | | 701 | 6 | ||
| 8 | | 5614 | 2. | ||||
| 8 | | 44914 |
| Complements in Radix r | Euclid's Division Algorithm | Base Conversion | Practice in two's complement | Java Primitive Data Types: | Integers | Reals | Homework |