So we know that computers use binary numbers to represent letters, symbols, etc. But we have never looked at how we can tell if a number is negative. There has to be a way because obviously computers use negative numbers. And since everything is binary, we can’t just tag on a “-” sign in front of the number. So let’s take a look.
The Plan
Ok, since we can only use 1’s and 0’s we’re going to have to use one of them to indicate that a number is negative. Let’s decide that if the first digit is a 1 then the number is negative, if it starts with 0 then it’s positive.
That obviously assumes that we have to know how long the number is. For example, we can agree that we are only using 3 digits for the number then 0110 would be 5, and 1110 would be -5. Get it? 1110 is not 14 because we said we are only using 3 digits for the number portion, so the first digit represents the sign.
Ones Complement
The above way is one method of representing negative numbers in binary. Another method we will look at is called ones complement.
Ones complement just means reversing every bit. Every 1 becomes 0, and every 0 becomes 1. That’s all there is to it. Lets see an example of finding a 4 digit ones complement number. To convert the number -6 to binary with ones complement we simply find positive 6 in binary: 0110. Then we just flip the bits to get 1001.
For finding any number x in binary we can follow these steps:
- if x is positive, simply convert to binary
- if x is negative convert to the positive value of x in binary
- find the ones complement
One thing that’s wierd about one’s complement is that there is two values for zero. That is because zero can be written as all 0’s or all 1’s.
Two’s Complement
Two’s Complement is the most common way of representing negative numbers in binary. It is similar to one’s complement but with a twist. We just need to take the one’s complememt of the number and then add one. Let’s see an example where we find -5.
0101 (5)
1010 (one’s complement)
1011 (add one -> becomes two’s complement) That’s all there is to it. One of the benefits of two’s complement is that there is only one way to have zero (unlike one’s complement.) Here’s why: if we’re using 4 bit numbers, then zero is 000. If we take the twos’s complement we first get 1111 then we add 1 to get 10000. Since the carry 1 goes in to the fifth position, we ignore it because we are using 4 digit numbers. This is called “ignoring the overflow.”
Two’s complement has another benefit and it has to do with how it handles addition; but we’ll save that for homework.
Homework
-
Create a chart that shows all 4 digit two’s complement numbers. Also, add it to it the positive and two’s complement of the numbers 68, 95, 236, and 313 as 8 digit binary numbers. (Knowledge)
-
Using examples, compare how to perform addition and subtraction using both one’s complment and two’s complement numbers. (Thinking)
-
DUE: Next class