Today’s problem is to express integers (negative numbers) in binary. Programmers represent integers in a way that is more clever than mathematicians, but it is confusing at first.
In mathematics, if you wish to write a negative number you add a ___ to the front of the number.
If you wish to write a positive number you add a ____ to the front of the number.
It makes sense that in binary,
we will just add a 1 in front of the negative numbers and
0 for the positive numbers to the front of the number.
Write +3 in binary: ________ Now, write –3 in binary: ________
Write 7 in binary, without a sign bit. _____
èWhat do you notice about your last two answers?
Ambiguity Solution:
To make things easier and less confusing, we will state how many bits the number needs to be. We will pad the front with zeros and stick the sign bit in front of everything.
8 bit length. +3: 00000011 sign bit: ____, other 7 bits: _____________
8 bit length. –3: 10000011 sign bit: ____, other 7 bits: _____________
8 bit length +7: 00000111 sign bit: ____, other 7 bits: _____________
8 bit length +15: _____________
8 bit length -15: _____________
èWrite the numbers from –3 to 0. (4 bits)
èWrite the numbers from 3 to 0 (4 bits)
èWhat two representations exist for zero?
èHow would you add +3 (8 bits) and +2 (8 bits)?
èHow would you add -3 (8 bits) and -2 (8 bits)?
èHow would you add +3 (8 bits) and -2 (8 bits)?
All of this can be done, but it is harder if the signs are different because you must subtract. Humans can make these kinds of adjustments easily, computers prefer to use one method for everything.
This made of representing integers in binary has two major errors that can not be solved:
1 __________________________________________
2 __________________________________________
The complement of a number is its negation. In binary you take each digit and subtract it from 1.
0101’s complement is 1010.
1111’s complement is 0000.
Find the complements of these binary numbers:
101011: ____________
101110: ____________
110100: ____________
001010: ____________
In decimal, you take each digit and subtract it from 9.
234’s complement is 765
913’s complement is 086
Find the decimal complements of these numbers:
358: ____________
495: ____________
819: ____________
602: ____________
In octal, you take each digit and subtract it from ___.
123’s complement is 654
704’s complement is 073
Find the octal complements of these numbers:
345: ____________
527: ____________
105: ____________
746: ____________
You should note that in one’s complement that the first digit is still the sign digit.
1 is for negative / positive (circle the correct)
0 is for negative / positive (circle the correct)
+7 (4 bits) is 0111
-7 (4 bits) is the complement: 1000
For +7, the first digit is still ___. For –7, the first digit is still ___
Try these:
+6 (4 bits, sign bit at front) ____________
-6 (+6’s complement) _______________
+3 (4 bits, sign bit at front) ____________
-3 (+3’s complement) _______________
+4 (4 bits, sign bit at front) ____________
-4 (+4’s complement) _______________
You will remember that we had a problem with our first attempt at integers that we had a positive zero and a negative zero.
+0 (4 bit) = 0000
-0 (4 bit) is the complement which is _______________
So the problem of two zeros still exists.
We also had a problem in our first attempt at integers in binary were we had to use a different method for adding a negative integer to a positive one. We wanted to use one way to add all integers in all situations.
So, in decimal, (-4) + (+1) = _______
Using 4 bits, and a sign bit, find:
+4: ____________
-4: ____________
+3: ____________
-3: ____________
+1: ____________
Convert the binary to decimal using your above work.
1001 = _________ in decimal
+0001 = _________ in decimal
1100 = _________ in decimal.
Now check the binary addition. Add in the carry numbers.
The binary addition was correct! And we didn’t have to do anything special to handle the negative. It handled itself. In Computer science talk, we say that “the addition operation is independent of sign”.
Let’s do some more examples. Do the adding in both binary and decimal. Check to see that your answers are correct:
(3) = _______
+(-2) = _______
= _______
(5) = _______
+(-1) = _______
= _______
(-7) = _______
+(+2) = _______
= _______
(-3) = _______
+(+1) = _______
= _______
This doesn’t quite work if both of the numbers are negative. Then you have to add one to the answer.
(-3) = _______
+(-2) = _______
= _______
(-5) = _______
+(-4) = _______
= _______
One’s complement is better.
Now we can : _______________________
But we still have two problems:
1 __________________________________________
2 __________________________________________
In this representation of negatives – the final solution – we will express negative numbers as a two’s complement.
To find the two’s complement, you
find the one’s complement (complement each bit) and
add one.
+7 (4 bits) = 0111
complement = 1000
add one = 1001
-7, therefore is 1001.
Find the 4 bit twos complement of the following:
7: ___________
6: ___________
5: ___________
4: ___________
3: ___________
2: ___________
1: ___________
-1: ___________
-2: ___________
-3: ___________
-4: ___________
-5: ___________
-6: ___________
-7: ___________
You should note that in two’s complement that the first digit is still the sign digit.
1 is for negative / positive (circle the correct)
0 is for negative / positive (circle the correct)
+0 (4 bit) is 0000
Let’s find –0.
o ones’ complement: _________
o add one: __________
o Because adding one makes there be more than four bits, we cut off the front one.
o cut off front one: _______
o -0 is __________, which is the same as +0.
Problem solved.
Add these numbers in twos complement. Do the adding in both binary and decimal. Check to see that your answers are correct:
(3) = _______
+(-2) = _______
= _______
(5) = _______
+(-1) = _______
= _______
(-7) = _______
+(+2) = _______
= _______
(-3) = _______
+(+1) = _______
= _______
Does it work if both numbers are positive?
(-3) = _______
+(-2) = _______
= _______
(-5) = _______
+(-4) = _______
= _______
Problem solved.
1. What is a sign bit?
2. Why do we need to specify the length of a number?
3. Summarize how to make a positive number:
4. How do you find ones’ complement?
5. How do you find two’s complement?
6. Why do we use two’s complement for negative numbers?