Hey! In this lesson we're gonna get negative.
No, not that kind of negative!
I'm more of a "the glass is incorrectly sized" kinda guy
anyway. We're going to talk about negative numbers in
how we store negative numbers in Java. If you're not sure how computer store negative numbers
differently,
you'll definitely wanna keep watching this video.
The goals for this lesson are to understand how we store negative numbers
in binary.
This is also how numbers are stored in Java. We're gonna use something called
two's complement numbers. Now let's start with what we know.
We talked about binary numbers in the last lesson, and how they're organized to
represent positive numbers.
Each digit represents a power of two. This is like the normal power
of 10 numbers we already use. So how does it
work with negative numbers? Well
your first thought might be to sacrifice the first digit, and use it as a sign bit.
Let's try that! Let's start with three bit numbers
and add -3 and 2. The first number will be
111. That will be our -3.
And then we'll add 010. That's our two.
Add it and we get 001 or one. Wait
-3 plus two. That didn't work!!
We should get 101 as a result. The answer is -1
Maybe if we add special code for the sign bit?
But how will we know when to do that programmatically? The problem with this
strategy is
it induces a bunch of cases. A case for two positive numbers
A case for two negative numbers. A case where the positive number is larger. Case where
negative number is larger.
A case where the positive and negative number add to zero.
This completely wrecks our simple adder.
It won't work. Okay time out!
Now I'm gonna take an aside. We need to talk about something else that might help
and come back to this problem later.
Imagine a clock. On the clock you have the numbers one
to twelve. If it's three o'clock and you add 12 hours
its still three o'clock. We're not looking at days here. Just the hour.
If we add three hours
that makes it six. Add fifteen hours
and it's still six.
If we subtract nine hours, yep
still six.
This works for multiplication too. You can try that one at home.
The reason this works is 3, -9,
and 15 are all equivalent numbers for a clock. It doesn't matter which one you use.
You get the same number in the end. The math way to say this is that they are
all
congruent modulo 12. Don't worry, that's not on the quiz.
So, back to binary land.
How can we use this information for our computer numbers. We do the same thing
for our numbers.
Imagine our computer holds 3 bits. We're only using 3 bits because
I want to show you every possible number. On a computer.
the numbers vary from 8 bits to 64 bits. That's
just too many to display here. Now think of the numbers as
positions on the clock. We'll map our numbers this way.
The first bit still means negative,
but the other bits are not how you'd represent positive numbers.
For example -4 is 100 and
-1 is 111. So the positive numbers look the same and
negative numbers look weird. What happens now? Let's try adding
-3 and 2 again. So we add 101
and 010. That gives us 111
which is -1! It works! No special logic.
It works because these are equivalent numbers in binary.
So how do we calculate the special number when we're using 16 bits?
Well there's really no magic to it. To change the sign of a number
all the bits are flipped, and you add one. Earlier we had -3.
So three in binary is
011. Step 1, flip the bits
to get 100. Then we add one.
So the compliment number is 101.
Just like on our clock. If this number were eight bits
the number would be 1111 1101.
So the number of bits in your mapping matter to calculate the correct
equivalent number. Luckily this is all handled for us behind-the-scenes.
However it is important to understand what your computer is doing.
So this is why we use two's complement for all numbers in Java.
There are no unsigned numbers in Java. Every counting number
type is doing this for negative numbers. For some of the larger number types
there are ways to use them unsigned, but we can't cover that until we get to number
classes.
That's a ways away. In the next lesson we'll actually look at out first type.
The byte.
Hey thanks for watching the video! There is a quick quiz for this on DeegeU.com
if you'd like to gauge how much you learned.
If you like the videos you are seeing please let me know by liking the video and
hitting the subscribe button for the DeegeU channel on YouTube. I'd
really appreciate that! If you have concerns or questions please leave them
in the comments below or on DeegeU.com.
There's a poll on the front page of DeegeU.com,
so you can let me know what topic is covered next.
Thanks for watching and see you in the next video!