sections in this module | City
College of San Francisco - CS270 Computer Architecture Module: Machine Basics |
module list |
Number Representation
Computer data is stored in binary. We think in decimal. Computer data numerically often is expressed in octal or hexadecimal. To effectively talk (and read) about computer data, we must be able to freely convert between these representations.
An n-bit binary number can have the value 0 to 2^n - 1. Just like
decimal numbers, where each digit represents a power of ten, a
binary number has each digit representing a power of 2. In fact,
for any base b and unsigned number xyz in that base, xyz has the
value z * b^0 + y * b^1 + x * b^2. If the base b
is less than 10, the usual digits are used. Of course the maximum
digit in any base b is (b-1), just like the maximum digit in base
10 is 9. For bases greater than 10, letters are used, starting
with A. For hexadecimal, lower- and upper-case letters like A and
a are the same value (in the case of A, decimal 10). For the
increasingly popular base64, upper- and lower-case alphabetic
characters have different values.
Here are a few examples using four-digit numbers (for simplicity)
base
(b) |
position values |
examples | |||
exponential |
decimal
value |
in
base b |
position
values (decimal) |
total
(decimal) |
|
10 |
10^3 10^2 10 1 |
1000 100 10 1 |
|||
2 |
2^3 2^2 2 1 |
8 4 2
1 |
1011 |
1*8 + 0*4 + 1*2 + 1*1 |
11 |
4 |
4^3 4^2 4 1 |
64 16 4 1 |
3203 |
3*64 + 2*16 + 0*4 + 3*1 |
227 |
8 |
8^3 8^2 8 1 |
512 64 8 1 |
2715 |
2*512+ 7*64 + 1*8 + 5*1 |
1485 |
16 |
16^3 16^2 16 1 |
4096 256 16 1 |
2AC8 |
2*4096+10*256+12*16+8*1 |
10952 |
One straightforward way to convert between two non-decimal bases is to convert the source number to decimal, then convert the decimal result to the target base. For example, if we take the number 437 base 8 (octal numbers are indicated by a leading zero, so, without specifying the radix, this number could be written as 0437), and we wanted to convert this to hexadecimal, the [silly] straightforward way to do so would be to convert it to decimal, then convert the decimal to hexadecimal:
convert 0437 to decimal: 4*8*8 + 3*8 + 7 = 287
convert 287 to
hexadecimal: take the largest power of 16 that it contains, 256,
which is 16*16: 287-256 leaves 31. This is one 16 and fifteen
left. Thus, 11F. (Just like octal numbers use a 0 prefix to indicate their base, hexadecimal numbers use the prefix 0x. Thus 11F would correctly be written as 0x11F. However, many authors do not bother with the 0x if the number is obviously hexadecimal, as is 11F.)
An easier way to do this is to express the octal number in binary. This is easy since each octal digit is three binary digits:
4 |
3 |
7 |
||||||
1 |
0 |
0 |
0 |
1 |
1 |
1 |
1 |
1 |
Then regroup the binary digits in groups of four for hexadecimal and express the result:
1 |
0 |
0 |
0 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
F |
This doesn't make too much difference with a small number, but suppose we wanted to express the number 0x3F56 in octal? (Just like a 0 prefix indicates an octal number, a 0x prefix indicates hexadecimal). Using our technique:
3 |
F |
5 |
6 |
||||||||||||
0 |
0 |
1 |
1 |
1 |
1 |
1 |
1 |
0 |
1 |
0 |
1 |
0 |
1 |
1 |
0 |
0 |
3 |
7 |
5 |
2 |
6 |
This was a lot easier than doing a calculation that begins with
multiplying 65536 by 3!
Of course, this type of conversion is only possible between bases that are powers-of-two: 2,4,8,16, but 2,8, and 16 are the most-often converted-between bases (besides decimal, of course)
Octal, binary, and hexadecimal numbers are always expressed as unsigned. When decoding decimal numbers, however, you must know if the number is to be interpreted as signed. We will discuss this in the next section.
Prev | This page was made entirely
with free software on linux: the Mozilla Project and Openoffice.org |
Next |