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
0001
0010
0011 -> +3
0100
0101
0110
0111
1000 -> 0
1001
1010
1011 -> -3
1100
1101
1110 -> -6
1111

Another method they came up with is called 1s complement

1111 -> 0
1110
1101
1100 -> -3
1011
1010
1001
1000

Lets add two numbers together

  0011
+ 1100
  ----
  1111 -> -0

Another example

  1
  0101 -> +5
+ 1100 -> -3
  ----
  0001 -> -0 + 1 + 1 = 2

So we have the following form of representing it:

x(1's complement + 1) = -x

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

So as range we have:

2n1+0+(2n11)

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?

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

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

Lets do another example:

+5   0101   
+7   1001
	 ----
	*0100  -> +4

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