Java Reference
In-Depth Information
Now here we are going to demand a little faith on your part and avoid getting into explanations of why
it works. We will just show you how the 2's complement form of a negative number can be constructed
from a positive value, and that it does work so you can prove it to yourself. Let's return to our previous
example where we need the 2's complement representation of -8. We start with +8 in binary:
0000 1000
We now flip each digit - if it is one make it zero, and vice versa:
1111 0111
This is called the 1's complement form, and if we now add 1 to this we will get the 2's complement form:
1111 0111
Add one to this
0000 0001
and we get:
1111 1000
Now this looks pretty similar to our representation of -8 we got from subtracting +12 from +4. So just to
be sure, let's try the original sum of adding -8 to +12:
+12 is
0000 1100
Our version of -8 is
1111 1000
and we get:
0000 0100
So the answer is 4 - magic! It works! The carry propagates through all the leftmost 1's, setting them
back to zero. One fell off the end, but we shouldn't worry about that. It's probably the one we borrowed
from off the end in the subtraction sum we did to get -8. In fact what is happening is that we are making
the assumption that the sign bit, 1 or 0, repeats forever to the left. Try a few examples of your own, you
will find it always works quite automatically. The really great thing is, it makes arithmetic very easy (and
fast) for your computer.
Floating Point Numbers
We often have to deal with very large numbers: the number of protons in the universe, for example,
which needs around 79 decimal digits. Clearly there are lots of situations where we need more than the
10 decimal digits we get from a 4 byte binary number. Equally, there are lots of very small numbers.
The amount of time in minutes it takes the typical car salesman to accept your offer on his 1982 Ford
LTD (and only covered 380,000 miles...). A mechanism for handling both these kinds of numbers is - as
you will have guessed from the title of this section - floating-point numbers.
A floating-point representation of a number is a decimal point followed by a fixed number of digits,
multiplied by a power of 10 to get the number you want. It's easier to demonstrate than explain, so let's take
some examples. The number 365 in normal decimal notation would be written in floating point form as:
Search WWH ::




Custom Search