| Overall Reading | |
|---|---|
| Brookshear: | pp. 24-25, 38-42 and Ch. 1.5 |
| Decker/Hirshfield: | pp. 203-206 and middle of p. 248 |
Outline:
As an example, how many |'s are given here?
| | | | | | | | | | | | | |
Answer: 14 (i.e., 10 + 4)
We represent an integer value by a series of digits, where each digit is from the set {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}.
Rightmost digit is the ones digit (i.e. 100)
Second from right is the tens digit (i.e. 101)
Third from right is the hundreds digit (i.e. 102)
Fourth from right is the thousands digit (i.e. 103)
Example: 4089 = 4*103 + 8*101 + 9*100.
Fact: You can represent 10k distinct
numbers using k decimal digits.
Fact: Every postive integer has a unique representation in
this system.
Why do you think humans chose to use base 10 as our number system?
(Hint: think about the term 'digit')
We will need b distinct alphabet symbols to represent the values from {0, ..., b-1}.
Rightmost digit counts b0 (i.e. one's)
Second from right is the b1 digit
Third from right is the b2 digit
Fourth from right is the b3 digit.
Fact: Every postive integer has a unique representation in this system.
For base 16, must have symbols to represent the values from {0, ..., 15}.
For hexadecimal, it is conventional to use characters
{A, B, C, D, E, F} in place of {10, 11, 12, 13, 14, 15).
(see pp. 24-25 of [Br])
In this case,
Rightmost digit represents 10 = 1's.
Second from right represents 11 = 1's.
Third from right represents 12 = 1's.
Fourth from right represents 13 = 1's.
In this unary number system, there is only one symbol used for each digit, and the number is represented simply by the number of digits used (essentially, like making scratchmarks on the wall...)
12(base 10) = | | | | | | | | | | | | (base 1)
Let's try some examples...
The beauty of an algorithm is that if you can follow the steps mindlessly, you will get the desired result.
Of course, it actually
may help your intuition to understand why the above algorithm
actually works. So let's examine the method more closely...
Let's begin be reexamining the decimal system.
Though base 10 might seem particularly convenient for humans, the most natural system for representing integers on a digital computer is the binary system (i.e. "base two").
From binary to decimal
Example, value "100101" can be decoded, right to left:
1*one + 0*two + 1*four + 0*eight + 0*sixteen + 1*thirtytwo
= 1 + 4 + 32 = 37
From decimal to binary
Let's convert 101(base 10) to binary.
As analogy, if you are used to working in base ten, it is easy to adapt
to "base thousand". After all,
$19,345,286 equals 286*1 + 345*1000 + 19*10002
In Computer Science, powers of two arise as convenient bases at times:
Every four bits in binary defines a digit in hexadecimal.
For instance a value of
10110011100 (base 2) equals
59C(base 16)
On the other hand, humans have a great deal of trouble converting directly between two unrelated bases (e.g. converting from base 6 to base 7). The reason is that the conversion algorithm above involves some primitive arithmetic operations, and as humans we are mostly comfortable with these primitives when working in decimal notation. Therefore, in such a case we probably find it easiest to convert a number from base 6 to base 10 and then from base 10 to base 7.
If doing this in binary, the bits after a decimal point represent 1/2's, then 1/4's, then 1/8's and so on.
However, fractions are generally represented in modern computers using what is termed floating-point notation, which is similar to scientific notation. This provides a better range of both very large and very small numbers. We will not discuss these in this course, but those interested may read Ch. 1.7 of [Br].
We talked about the algorithms we learned in grade school for doing basic arithmetic in decimal notation.
Those same algorithms can be applied in any of these bases!
[lets practice]
357257 + 62545 --------How do we do this if the numbers are represented in base 10?
If you have time, you can try to do multiplication in another base.
In particular, the standard calculator application that comes as part of Windows will convert numbers between decimal, binary, octal or hexadecimal. This calculator can usually be started by looking in the Start -> Programs -> Accessories menu. When it begins, it generally looks like a very simple calculator. However, there is a menu title "View" at the top of the calculator that lets you select between "Standard" and "Scientific" views. Select the Scientific view.
In scientific mode, you will see a box that allows you to pick between one of four number systems: Hex(adecimal), Dec(imal), Oct(al), or Bin(ary). If you want it to convert from decimal to binary, select decimal mode, then type in a decimal number, then click on binary mode. In the same way you can convert from binary to octal, and so on. You can also perform all of the arithmetic operations while working in any of the number systems. Please note, however, that this calculator does not handle fractional values in any base other than decimal.
One warning: When working outside of decimal notation, the calculator can only handle values which fit in 32 bits or less (limit is slightly less than 4.3 billion). Also, I strongly recommend that you keep the selection on the right as "Dword" when working in one of the other bases.