09-11-25 Lab 5
Class: CSCE-312
Notes:
How does 2's complement came about
We need a way to show the sign bit for negative binary numbers
- 0000 -> 0
- 1111 -> 15
0000 -> 0
0001
0010
0011 -> +3
0100
0101
0110
0111
- If you want to show the negative numbers, just change the first byte from 0 to 1
1000 -> 0
1001
1010
1011 -> -3
1100
1101
1110 -> -6
1111
- This system has a couple of problems
- You now have two versions of zeros, 0 and -0
- You are losing one number
Another method they came up with is called 1s complement
- If you want a positive number and you want to change it to a negative version, just invert all the bits
1111 -> 0
1110
1101
1100 -> -3
1011
1010
1001
1000
- One beautiful finding
Lets add two numbers together
0011
+ 1100
----
1111 -> -0
Another example
1
0101 -> +5
+ 1100 -> -3
----
0001 -> -0 + 1 + 1 = 2
- The finding was that if you add one to the thing after doing its complement, then you actually get the number you want.
So we have the following form of representing it:
x(1's complement + 1) = -x
- Also called 2's complement
Getting the 2's complement of numbers
+0 0000 0000 -0
+1 0001 1111 -1
+2 0010 1110 -2
+3 0011 1101 -3
+4 0100 1100 -4
+5 0101 1011 -5
+6 0110 1010 -6
+7 0111 1001 -7
1000 -8
- Now in twos complement we can show one more number as well
- The problem is solved
So as range we have:
Just remember that in 2's complement the bits by value are
-8 4 2 1
-------
0 0 0 0
...
1 0 0 1 <- -8 + 1 = -7
Full adders and 2's complement
Lets say now we have 2 four bit numbers, how many full adders do we need?
- Each full adder is capable to manage 2 bits
- We will need 4 full adders
A - B -> (A) + (-B)
This is the exact circuit we use inside an ALU
A3 B1 A2 B2 A1 B1 A0 B0
| |- | |- | | - | |----- (XOR) Control
------- ------- ------- ------- |
| | | | | | | | |
out---- | FA4 | - | FA3 | - | FA2 | - | FA1 | --- +1
| | | | | | | | | |
---XOR ------- | ------- ------- -------
|------- | -- | | | |
sum3 sum2 sum1 sum0
- For B, we just get our control, Xor that with B, and pass it to each adder
- Now if we do A - B = A + (B') + 1
- This is the circuit we use to perform addition and subtraction with 2's complement
- The XOR is the overflow in case of any
Overflow
- The result you are seeing is not correct, you need more bits for it to show, you do not have enough bits
- In unsigned binary, it shows overflow
In 2's complement the story is a little bit different
n = 4 -8 -> +2
Lets say that we want to add:
+5 0101
+2 0111
----
*1100 -> -4
- How can your computer understand that this result is not correct
- There is a good indication:
- There are both positive numbers because of their MSB, and the result is negative! how?! that could be a good indicator
- There is a good indication:
Lets do another example:
+5 0101
+7 1001
----
*0100 -> +4
- Again an overflow!
- What is the indication here?
- That the two numbers are negative but the result is positive! that i impossible.
- If you have a way to store that bit elsewhere operations would not overflow, but sometimes there is no access to memory in order to store that extra overflow bit
To represent it in digital logic, take the output of the full adders circuit and XOR it with the carry, that is the overflow
Let's do an example where we do not see overflow
1
+5 0101
-7 1001
----
1110 -> -2
- If you want to see this, just convert it to a positive number
- It should be +2 after your conversion check
- This is the correct result we want, there is no overflow
- You can check that with XOR
- The output carry is 0, the previous carry is 0
- 0 XOR 0 = 0
- The output carry is 0, the previous carry is 0
- You can check that with XOR