transcript - New Mexico Computer Science for All

advertisement
Bites and Bytes: Numbers
[Slide 1] Let’s start with a very basic idea of how do we represent numbers.
We’ll start with the more familiar Base 10 arithmetic. We say a number is some
sequence of digits. Let’s just talk about whole numbers or integers for a minute.
It’s a sequence of digits. Each digit is between 0 and 9. I say, what is the value of
that number? This is a format known as a positional notation.
What each digit means depends on where its position is in that sequence, so that
if I look at a number abc here, that we’d say the a is in the third position.
Therefore, it says that the value of this … how many I really have, which is
independent of the representation … is I have 100 times whatever digit I have for
a, plus 10 times whatever digit I have for b, and how many … this should be a c
here … plus c times how many I have in that last place.
With just a little notation to make it a little simpler … and again, there should be
a c here, not a b … is we’ll use the exponent notation most of the time, so 100 is
102, and 10 is 101, and 1 is 100.
[Slide 2] What about fractions? If I give you a fractional number, say, 123.456, we
can continue what we had for whole numbers and just say everything on the left
of the decimal point obeys the rule that we just had, and everything to the right
of the decimal point gets multiplied by 1 over powers of 10. So, .4 here is 4/10.
The five, the second one over from the decimal point, is 5/100, and this is
4/1000. We can get the value of that by saying you have 1 times 102 plus 2 times
10, plus 3 times 1, and then 4 times 1/10, and so on for the 5 and the 6.
That’s fairly routine. We understand that in our head. It’s just one of many
representations we can have. This one happens to be one of the Base 10
representations.
[Slide 3] What about if I want to generalize that, and say, what about an arbitrary
base? Let’s say I want to do Base 15, or actually, an old one is Base 12. Base 12 is
an interesting one, because it comes up. You say, why does my clock work on
Base 12 when everything else is Base 10? Or, sometimes when you’re working
with geography, and longitude and latitude, you get minutes and seconds.
Where did that come from? Most likely, that came from that the moon goes
around the Earth about 12 times in a year. There are various other explanations
for that.
There are other bases that do come up. The ones we’re going to worry about
when we talk about computers are typically numbers that are a power of 2, so
Base 2. Base 8 comes up. We won’t talk about that today. We’ll talk a little bit
about Base 16. The general form is that if you have a base-end number, then the
Bits-Bytes_Numbers1
Page 1 of 3
digits can be anywhere from 0, to 1, to a 1 minus whatever the base is. Then we
compute everything else, just as we did with Base 10.
If I had a number abc.def, the a then multiplies the base times the second
power, because it’s 3 to the left. The b multiplies the base end to the first power.
The c … again, I once more made the same mistake here. This should be c …
multiplies N0, which is going to be 1. Then the fractional parts use 1/N for the
first, the d, and 1/N2 for the e, and 1/N3 for the f. We can do exactly as we did
with Base 10 to compute what the actual value of that representation is.
[Slide 4] The problem is, if I look at some sequence of digits, I really don’t know
from that what base it really is, because I can look at a sequence like this,
101101, and say, gee, if I’m talking about Base 10, that’s a million, ten thousand,
one hundred and one. But if it’s in Base 2, it’s going to mean something else, and
if it’s Base 5, or Base 8, or Base 16, it’s going to be something else.
What we’ll do is we’ll use a subscript if there’s any ambiguity. This number in
Base 2 is 45 in Base 10. It’s 32 plus 16 plus 4 plus 1, using the rules that we just
had. Two different representations of the same number.
Just a little note, because this comes up all the time, is two very convenient ones
… and you see this used all the time … is that K, which in normal representation
in science, usually means a thousand, and M means a million. When we’re
dealing with Base 2, they’ll almost always, when you see them in any kind of
computer literature, will refer to the closest power of 2, and 2 10 is 1024, and M is
220, which is 10242. It’s bigger than a million but not too much bigger.
When you see things quoted, for example, how many megs of memory do you
have in your system, or what is the speed of your internet connection, you’ll
generally see K or M, where they’ll mean 1024 or 10242.
[Slide 5] What about arithmetic? We’re going to do this just very, very simply.
I’m going to talk about addition, and you can worry about subtraction and
multiplication, and things. A little bit later, you’ll do it yourself.
It’s really the same as what we do in Base 10, just realizing that in Base N, we
only have N minus 1 digits. In Base 2, we only have as our digits or bits a 0 and 1.
We have the rule 0 plus 0 is 0, 1 plus 0 or 0 plus 1 gives us a 1. If we add 1 and 1,
that gives us 2, but 2, to represent in Base 2, is 10, because I have to put only a 1
or a 0 here, so that’s just the carry. It’s just like if we had Base 10 and we had to
carry, when two numbers summed up to be 10 or greater. I can now do this
addition exactly as I would do it in Base 10: 1 plus 0 is 1, 0 plus 1 is 1, 1 plus 1 is a
0 and a carry of 1, then a 1 and a 1, and so on. I’ll get, I think, this is the correct
result for adding those two binary numbers.
Bits-Bytes_Numbers1
Page 2 of 3
If you want to take a little break here, [Slide 6] I’ll leave you with a couple of
questions.
Can you come up with an algorithm for doing that? What I mean by an algorithm
is, can you come up with an algorithm that starts at the, let’s … I’ll give you a hint
… you want to start at the least significant bit of this number on the right, and
give me an algorithm that, in terms of the digits, represent this. Say this is a0, a1,
a2, a3, a4, a5, likewise, b0. Give me an algorithm for doing addition in terms of a
step-by-step procedure for adding two binary numbers. That should be fairly
simple.
What about subtraction? Generally, if you know how to do addition, you should
know how to do subtraction. I can argue the same thing about multiplication. If
you know how to do addition, you should be able to do multiplication. You
should be able to do division, too, but that gets a little uglier. You might want to
think about that, too.
If you want to take a break here and stop the playback, you can do that and think
about how you might do these two or three operations.
Bits-Bytes_Numbers1
Page 3 of 3
Download