Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

42
Binary Arithmetic

Transcript of Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

Page 1: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

Binary Arithmetic

Page 2: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

2

Outline

• Binary Addition• 2’s complement• Binary Subtraction• Half Adder • Logisim

Page 3: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

3

Binary Addition

• How to add two binary numbers?• Single bit:

0+ 0

0

0+ 1

1

1+ 0

1

1+ 110

carry bit

Page 4: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

4

Binary Addition

• What is a carry bit?• Same idea as “carry the 1” in decimal arithmetic:

13+ 9

1013

+ 92

1013

+ 922

Page 5: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

5

Binary Addition

• carry bit gets “carried over” to the next bit (to the left..)• Just like decimal addition

0+ 0

0

0+ 1

1

1+ 0

1

1+ 110

carry bit

Page 6: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

6

Binary Addition

• Example #1

0100 0011+ 0000

1010

Page 7: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

7

Binary Addition

• Example #1

0100 0011+ 0000

10100100 1101

carry bit

Page 8: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

8

Binary Addition

• Example #1• You can always check your work by converting to decimal

0100 0011+ 0000

10100100 1101

67+

1077

Page 9: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

9

Binary Addition

• Example #2

1100 0110+ 0100

1010

Page 10: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

10

Binary Addition

• Example #2

1001 11001100 0110

+ 0100 1010

1 0001 00009th

bit!?!

Page 11: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

11

Binary Addition

• Example #2• If we must store the answer in a single byte, 9th bit gets ignored…• This is known as overflow

1100 0110+ 0100

10101 0001

0000

Page 12: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

12

Binary Addition

• Example #2

198+ 74

16

16 !?! Shouldn’t the answer be 272?

1100 0110+ 0100

10101 0001

0000

Page 13: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

13

Binary Addition

• What’s the largest number that can be stored in 8-bits?

1111 1111

???

Page 14: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

14

Binary Addition

• What’s the largest number that can be stored in 8-bits?

• 272 is greater than 255• 272 cannot be represented using 8 bits• Has this ever happened to you?

1111 1111

255

Page 15: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

15

Outline

• Binary Addition• 2’s complement• Binary Subtraction• Half Adder • Logisim

Page 16: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

16

2’s Complement

• 8-bits can be used to represent numbers between 0 and 255• How do we represent negative numbers in binary?

• Drumroll…. 2’s complement!!• Makes addition, subtraction, multiplication easier• Most common way to represent signed numbers

• Signed: positive AND negative numbers

• And no, it’s not 2’s compliment• “Hey 2, your hair looks nice today..”

Page 17: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

17

2’s Complement

• In 2’s complement system, the leftmost bit indicates the sign• 0 for positive• 1 for negative

• When the leftmost bit is 0, the remaining bits are interpreted as before• 0000 0001 => 1• 0111 1111 => 127

Page 18: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

18

2’s Complement

• When the leftmost bit is 1, we do the following to obtain the signed decimal representation:

1. Complement (invert) the binary digits (0 => 1; 1 => 0)2. Convert binary digits to decimal number3. Multiply by -14. Subtract 1

Page 19: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

19

2’s Complement

• Example #1: What is the 2’s complement value of 1100 0110?

1100 0110

1. Invert bits: 0011 1001

2. Convert to decimal: 57

3. Multiple by -1: -574. Subtract 1: -58

Page 20: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

20

2’s Complement

• Example #2: What is the 2’s complement value of 1001 1001?• Your turn!

1001 1001

Page 21: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

21

2’s Complement

• Example #2: What is the 2’s complement value of 1001 1001?

1001 1001

1. Invert bits: 0110 0110

2. Convert to decimal: 102

3. Multiple by -1: -1024. Subtract 1: -103

Page 22: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

22

2’s Complement

• Decimal to 2’s complement• If the number is positive:

1. leftmost bit is 02. remaining bits identical to unsigned binary number

• E.g., Represent the number 97 using 8-bits, 2’s complement

97 = 64 + 32 + 1= 26 + 25 + 20

= 0110 0001

Page 23: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

23

2’s Complement

• Decimal to 2’s complement

• If decimal number is negative:1. add 12. multiply by -1 (to create positive number)3. create binary sequence4. invert bits

Page 24: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

24

2’s Complement

• Example #3: represent -97 using 8-bits, 2’s complement

1. add 1: -97 + 1 = -96

2. multiply by -1: -96 × -1 = 96

3. get binary: 96 = 0110 0000

4. invert bits: 1001 1111

-97 => 1001 1111

Page 25: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

25

2’s Complement

• Example #4: represent -123 using 8-bits, 2’s complement

1. add 1: -123 + 1 = -122

2. multiply by -1: -122 × -1 = 122

3. get binary: 122 = 0111 1010

4. invert bits: 1000 0101

-123 => 1000 0101

Page 26: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

26

Break Time!!!

Page 27: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

27

Outline

• Binary Addition• 2’s complement• Binary Subtraction• Half Adder • Logisim

Page 28: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

28

Binary Subtraction

• To subtract two binary numbers, X – Y,1. Invert Y => Y’2. Add 1 to Y’3. Add X + Y’

• We are basically taking the 2’s complement of Y (Y’) before adding it to X

Page 29: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

29

Binary Subtraction

• Example #5: 0010 0001- 0000

10101. invert Y 00001010 => 11110101

2. add 1 to Y’11110101 + 00000001

= 11110110

3. add Y’ to X0010 0001

+ 1111 0110

1 0001 0111

this 9th bit 1 gets ignored

Page 30: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

30

Binary Subtraction

• It’s always good to verify your work…

0010 0001- 0000

10100001 0111

33- 10

23

Page 31: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

31

Binary Subtraction

• Example #6

0001 1001- 1110

0010

Page 32: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

32

Binary Subtraction

• Example #6 0001 1001- 1110

00101. invert Y 1110 0010 => 0001 1101

2. add 1 to Y’0001 1101 + 0000 0001 =

0001 1110

3. add Y’ to X0001 1001

+ 0001 1110 0011 0111

Page 33: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

33

Binary Subtraction

• Verify…

0001 1001- 1110

00100011 0111

25- -30

55

Page 34: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

34

Outline

• Binary Addition• 2’s complement• Binary Subtraction• Half Adder • Logisim

Page 35: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

Half Adder

• Can use logic gates to construct adder circuit• Circuit is capable of binary addition

• Half Adder has two inputs (A, B) and two outputs (S, C)• S: sum• C: carry

35

Half Adder

A

B

S

C

Page 36: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

36

Half Adder

• Truth table for Half Adder

Half Adder

A

B

S

C

A B S C

0 0 0 0

0 1 1 0

1 0 1 0

1 1 0 1

S : sum of A + B

C : carry bit

Page 37: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

37

Half Adder

• Logic gate circuit for Half Adder?• A, B inputs• S, C outputs A B S C

0 0 0 0

0 1 1 0

1 0 1 0

1 1 0 1

Page 38: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

38

Half Adder

• Logic gate circuit for Half Adder?• A, B inputs• S, C outputs A B S C

0 0 0 0

0 1 1 0

1 0 1 0

1 1 0 1

Page 39: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

39

Half Adder

• How would you expand the Half Adder to create a:• 2-bit adder?• 4-bit adder?• 8-bit adder?• …?

• This will be part of your homework…

Page 40: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

40

Outline

• Binary Addition• 2’s complement• Binary Subtraction• Half Adder • Logisim

Page 41: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

41

Logisim

• Logisim is FREE logic gate simulation software• Please download / install this software

• http://www.cburch.com/logisim/index.html• It should be on the lab machines, too

Page 42: Binary Arithmetic. Outline Binary Addition 2’s complement Binary Subtraction Half Adder Logisim 1.

42

Next Steps

• Download / install Logisim• Complete homework #2

• Data representation• Binary arithmetic• Create (useful) logic gate circuits using Logisim

• Next lecture: Micro-architecture