Can express a number in base b by repeatedly dividing by b
and recording the remainder. For example to express 21 in base 3:
21 / 3
= 7
= Remainder
0
7 / 3
= 2
= Remainder
1
2 / 3
= 0
= Remainder
2.
So the remainders are 0,1,2 and the number in base 3 is these
in reverse.
(21)10 = (210)3.
We can check that this is the case since:
(abc)3 = a*b2+b*b+c,
so (210)3 = 2*32+1*3+0 = 18+3 = 21.
Computers deal with bits, which are binary. They work with
collections of these, 32 in general in current machines.
Everything is represented in computers using bits, including numbers.
One way in which they are stored is in binary. 5 would be converted to
binary, (101)2 which can easily be stored using 32 binary digits.
This is is limited since the largest number we can represent is
then 232 which is not big enough (around 4 billion).
We can not deal with fractions either this way.
The other approach is to use scientific notation. Here we
express 3456 as 3.456*103. There are 2 parts to this, the
103 one which is called the exponent and the 3.456 part which
is called the mantissa.
We use some of our 32 bits for the mantissa and the rest for the
exponent. This means that we only store around 7 digits of our mantissa
BUT we also store a 3 digit exponent. The advantage of this is that
the range of numbers which we can represent is far, far greater.
The largest number we can represent is now 1038.
We can also store fractions down to 10-39 which means that we
can deal with fractions.
These benefits come at a slight cost though, which we consider next.
We can now deal with very large and very small numbers, but
we only keep track of around 7 digits. This means that any changes made
to the 8th or beyond digit is lost.
We can deal with milimeters or kilometers, but if our units are
kilometers milimeters are 0.000001 or in the 6th decimal place. If we
wanted to add this to 10km though it would be pushed to the 7th decimal
place since our exponent would increase by one.
This shows that the limitation is not a great one but you should be
aware of it.
This means that when on your calculator you divide 11/9, all the digits
might not be correct(it should be doing things in double where 15 digits
are correct). Just because a computer can print out 50 decimal places, it doesn't
mean that it means anything!
Octal, Hex and Binary
Since 8 is we can 23:
(131)8 = 1*82+3*8+1 = 1*26+3*23+1 = (001011001)2.
In other words we can take each octal digit and write it with 3
binary ones staight away. So
(244)8 = (010100100)2,
since 2 = (010)2 and 4 = (100)2. Check that this is the case.
We can go in reverse, if we have a binary number we can take 3
binary digits at a time and express it in octal. So
(111010001)2 = (721)8.
This means that (100100001)2 is what in octal?
Using this we can convert decimal numbers first to octal
by dividing by 8(less division required) and then using the above
approach to convert to binary quickly.
Similarly hex(base 16) can consists of 4 binary digits. Can convert
(A9F)16 = (101110011111)2
or say that
(001110011100)2 = (39C)16.
If have 32-bits on a computer we can convert easily to octal or hex
which we now know how to deal with. 4 binary digits correspond to one
hex, so 8 hex corresponds to 32 bits.
ASCII
Everything is stored as bits on a computer, not just numbers.
ASCII (American Standard Code for Information Interchange) is the
standard way to encode letters and symbols using bits, 7 in fact. This means
that we have 27 = 128 letters as opposed to the usual 26(there is unicode
now which extends this to include other alphabets as well for other languages).
Here, in ASCII, we attribute a number to each letter in lower and upper
case. Then to symbols like > and ? as well as numbers like 8.
Fractions
We have binary points in base 2 so fractions can be represented as
0.b1b2b3...,
but how do we determine b1, b2 and b3?
We multiply by 2 to move the binary point to the right. Then the fraction multiplied
by 2 is either less than 1 or not. If it is then b1 = 0, if it isn't then b1 = 1.
We repeat the process to obtain b2 and so on.
So
2/3
= 0.b1b2b3...
4/3
= b1.b2b3...,
which means that b1 = 1 since 4/3 > 1. Now we have
1/3
= 0.b2b3...
2/3
= b2.b3...,
which means that b2 = 0 since 2/3 < 1 and
2/3
= b3....
4/3
= b3....,
which means that b3 = 1 since 4/3 > 1. So since the pattern is clear
we have
2/3 = (0.10101010101...)2,
since 10 is repeated. We express this as
(0.
01
)2.
Problems(will do in class tomorrow, but try it yourself. Look at the notes):
What is (315)6?
Convert 711 into base 3.
If we used base 16 computers, what range of integers could
we express? What range of numbers using floating points?