number systems, base conversion, and signed-numbers...

32
Number Systems, Base Conversion, and Signed-Numbers (Class 1.1) CSE 2441 – Introduction to Digital Logic Fall 2012 Instructor – Bill Carroll, Professor of CSE

Upload: others

Post on 17-Mar-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Number Systems, Base Conversion, and Signed-Numbers …crystal.uta.edu/~carroll/cse2441/uploads/467DEA45-82DF-5992-F58A-E2C01F5FD65C_.pdfBase Conversion (8) • When B = Ak • Algorithm

Number Systems, Base Conversion, and Signed-Numbers

(Class 1.1)

CSE 2441 – Introduction to Digital Logic

Fall 2012

Instructor – Bill Carroll, Professor of CSE

Page 2: Number Systems, Base Conversion, and Signed-Numbers …crystal.uta.edu/~carroll/cse2441/uploads/467DEA45-82DF-5992-F58A-E2C01F5FD65C_.pdfBase Conversion (8) • When B = Ak • Algorithm

Today’s Topics

• Important number systems

• Binary arithmetic

• Base conversion

• Signed number representation

• Two’s complement number system

Page 3: Number Systems, Base Conversion, and Signed-Numbers …crystal.uta.edu/~carroll/cse2441/uploads/467DEA45-82DF-5992-F58A-E2C01F5FD65C_.pdfBase Conversion (8) • When B = Ak • Algorithm

Number Systems

• Decimal numbers – Digits = {0,1,2,3,4,5,6,7,8,9} – (26.75)10 = 2 x 102 + 6 x 100 + 7 x 10-1 + 5 x 10-2

• Binary numbers – Digits = {0, 1} – (11010.11)2 = 1 x 24 + 1 x 23 + 0 x 22 + 1 x 21 + 0 x 20 + 1 x 2-1 + 1 x 2-2 = (26.75)10

• Octal numbers – Digits = {0, 1, 2, 3, 4, 5, 6, 7} – (127.4)8 = 1 x 82 + 2 x 81 + 7 x 80 + 4 x 8-1 = (87.5)10

• Hexadecimal numbers – Digits = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F} – (B65F)16 = 11 x 163 + 6 x 162 + 5 x 161 + 15 x 160 = (46,687)10

Page 4: Number Systems, Base Conversion, and Signed-Numbers …crystal.uta.edu/~carroll/cse2441/uploads/467DEA45-82DF-5992-F58A-E2C01F5FD65C_.pdfBase Conversion (8) • When B = Ak • Algorithm

Important Number Systems

Decimal Binary Octal Hexadecimal

0 0 0 0

1 1 1 1

2 10 2 2

3 11 3 3

100 4 4

5 101 5 5

6 110 6 6

7 111 7 7

8 1000 10 8

9 1001 11 9

10 1010 12 A

11 1011 13 B

12 1100 14 C

13 1101 15 D

14 1110 16 E

15 1111 1 F

16 10000 20 10

Page 5: Number Systems, Base Conversion, and Signed-Numbers …crystal.uta.edu/~carroll/cse2441/uploads/467DEA45-82DF-5992-F58A-E2C01F5FD65C_.pdfBase Conversion (8) • When B = Ak • Algorithm

Representation of Numbers

• Positional Notation N = (an-1an-2 ... a1a0 . a-1a-2 ... a-m)r (1.1) where . = radix point r = radix or base n = number of integer digits to the left of the radix point m = number of fractional digits to the right of the radix point an-1 = most significant digit (MSD) a-m = least significant digit (LSD) • Polynomial Notation (Series Representation) N = an-1 x rn-1 + an-2 x rn-2 + ... + a0 x r0 + a-1 x r-1 ... + a-m x r-m = (1.2) • N = (251.41)10 = 2 x 102 + 5 x 101 + 1 x 100 + 4 x 10-1 + 1 x 10-2

a rii

i m

n

1

Page 6: Number Systems, Base Conversion, and Signed-Numbers …crystal.uta.edu/~carroll/cse2441/uploads/467DEA45-82DF-5992-F58A-E2C01F5FD65C_.pdfBase Conversion (8) • When B = Ak • Algorithm

Binary Arithmetic

Addition

111011 Carries

101011 Augend

+ 11001 Addend

1000100

Subtraction

0 1 10 0 10 Borrows

1 0 0 1 0 1 Minuend

- 1 1 0 1 1

Subtrahend

1 0 1 0

+ 0 1

0 0 1

1 1 10

Page 7: Number Systems, Base Conversion, and Signed-Numbers …crystal.uta.edu/~carroll/cse2441/uploads/467DEA45-82DF-5992-F58A-E2C01F5FD65C_.pdfBase Conversion (8) • When B = Ak • Algorithm

7

Binary Arithmetic (2)

Multiplication Division

1 1 0 1 0 Multiplicand

x 1 0 1 0 Multiplier

0 0 0 0 0

1 1 0 1 0

0 0 0 0 0

1 1 0 1 0

1 0 0 0 0 0 1 0 0 Product

1 0 0 1 1 1 1 1 0 1

1 0 0 1

1 1 0 0

1 0 0 1

1 1 1

1 1 0 QuotientDividend

Remainder

Divider

X 0 1

0 0 0

1 0 1

Page 8: Number Systems, Base Conversion, and Signed-Numbers …crystal.uta.edu/~carroll/cse2441/uploads/467DEA45-82DF-5992-F58A-E2C01F5FD65C_.pdfBase Conversion (8) • When B = Ak • Algorithm

Hexadecimal Arithmetic

Addition

1 0 1 1 Carries

5 B A9 Augend

+ D 0 5 8 Addend

1 2 C 0 1 Sum

Subtraction

9 10 A 10 Borrows

A 5 B 9 Minuend

+ 5 8 0 D Subtrahend

4 D A C Difference

See Table 1.5 in text for hex addition table.

Page 9: Number Systems, Base Conversion, and Signed-Numbers …crystal.uta.edu/~carroll/cse2441/uploads/467DEA45-82DF-5992-F58A-E2C01F5FD65C_.pdfBase Conversion (8) • When B = Ak • Algorithm

Hexadecimal Arithmetic (2)

Multiplication

B9A5 Multiplicand

x D50 Multiplier

3A0390 Partial products

96D61

9A76490 Product

Division

See Table 1.5 in text for hex multiplication table.

B9 57F6D79B

50F

706681

85D7F3

6A Remainder

Dividend

Quotient

Divider

Page 10: Number Systems, Base Conversion, and Signed-Numbers …crystal.uta.edu/~carroll/cse2441/uploads/467DEA45-82DF-5992-F58A-E2C01F5FD65C_.pdfBase Conversion (8) • When B = Ak • Algorithm

Base Conversion (1)

• Series Substitution Method – Expanded form of polynomial representation: N = an-1rn-1 + … + a0r0 + a-1r-1 + … + a-mr-m (1.3) – Conversation Procedure (base A to base B)

• Represent the number in base A in the format of Eq. 1.3. • Evaluate the series using base B arithmetic.

– Examples: • (11010)2 ( ? )10 N = 124 + 123 + 022 + 121 + 020 = (16)10 + (8)10 + 0 + (2)10 + 0 = (26)10 • (627)8 ( ? )10 N = 682 + 281 + 780 = (384)10 + (16)10 + (7)10 = (407)10

Page 11: Number Systems, Base Conversion, and Signed-Numbers …crystal.uta.edu/~carroll/cse2441/uploads/467DEA45-82DF-5992-F58A-E2C01F5FD65C_.pdfBase Conversion (8) • When B = Ak • Algorithm

Base Conversion (2)

• Radix Divide Method – Used to convert the integer in base A to the equivalent base B integer. – Underlying theory:

• (NI)A = bn-1Bn-1 + … + b0B0 (1.4) Here, bi’s represents the digits of (NI)B in base A.

• NI / B (bn-1Bn-1 + … + b1B1 + b0B0 ) / B = (Quotient Q1: bn-1Bn-2 + … + b1B0 ) + (Remainder R0: b0)

• In general, (bi)A is the remainder Ri when Qi is divided by (B)A.

– Conversion Procedure 1. Divide (NI)B by (B)A, producing Q1 and R0. R0 is the least significant digit, d0, of

the result. 2. Compute di, for i = 1 … n - 1, by dividing Qi by (B)A, producing Qi+1 and Ri, which

represents di. 3. Stop when Qi+1 = 0.

Page 12: Number Systems, Base Conversion, and Signed-Numbers …crystal.uta.edu/~carroll/cse2441/uploads/467DEA45-82DF-5992-F58A-E2C01F5FD65C_.pdfBase Conversion (8) • When B = Ak • Algorithm

Base Conversion (3)

Radix divide examples

3158

398480

374

LSD

MSD

315161916

1160

B31

LSD

MSD

(315)10 = (13B)16 (315)10 = (473)8

Page 13: Number Systems, Base Conversion, and Signed-Numbers …crystal.uta.edu/~carroll/cse2441/uploads/467DEA45-82DF-5992-F58A-E2C01F5FD65C_.pdfBase Conversion (8) • When B = Ak • Algorithm

Base Conversion (4)

Radix Multiply Method – Used to convert fractions. – Underlying theory:

• (NF)A = b-1B-1 + b-2B-2 + … + b-mB-m (1.5) Here, (NF)A is a fraction in base A and bi’s are the digits of (NF)B in base A. • B NF = B (b-1B-1 + b-2B-2 + … + b-mB-m ) = (Integer I-1: b-1) + (Fraction F-2: b-2B-1 + … + b-mB-(m-1)) • In general, (bi)A is the integer part I-i, of the product of F-(i+1) (BA).

– Conversion Procedure 1. Let F-1 = (NF)A. 2. Compute digits (b-i)A, for i = 1 … m, by multiplying Fi by (B)A, producing integer I-i, which represents (b-i)A, and fraction F-(i+1). 3. Convert each digits (b-i)A to base B.

Page 14: Number Systems, Base Conversion, and Signed-Numbers …crystal.uta.edu/~carroll/cse2441/uploads/467DEA45-82DF-5992-F58A-E2C01F5FD65C_.pdfBase Conversion (8) • When B = Ak • Algorithm

Base Conversion (5)

Radix Multiply Examples • (0.479)10 = (0.3651…)8 MSD 3.832 0.479 8 6.656 0.832 8 5.248 0.656 8 LSD 1.984 0.248 8 … • (0.479)10 = (0.0111…)2 MSD 0.9580 0.479 2 1.9160 0.9580 2 1.8320 0.9160 2 LSD 1.6640 0.8320 2 …

Page 15: Number Systems, Base Conversion, and Signed-Numbers …crystal.uta.edu/~carroll/cse2441/uploads/467DEA45-82DF-5992-F58A-E2C01F5FD65C_.pdfBase Conversion (8) • When B = Ak • Algorithm

Base Conversion (6)

• General Conversion Algorithm – Algorithm 1.1

To convert a number N from base A to base B, use (a) the series substitution method with base B arithmetic, or (b) the radix divide or multiply method with base A arithmetic.

– Algorithm 1.2

To convert a number N from base A to base B, use (a) the series substitution method with base 10 arithmetic to

convert N from base A to base 10, and (b) the radix divide or multiply method with decimal arithmetic to

convert N from base 10 to base B.

• Algorithm 1.2 is longer, but easier and less error prone.

Page 16: Number Systems, Base Conversion, and Signed-Numbers …crystal.uta.edu/~carroll/cse2441/uploads/467DEA45-82DF-5992-F58A-E2C01F5FD65C_.pdfBase Conversion (8) • When B = Ak • Algorithm

Base Conversation (7)

Example of Algorithm 1.2

(18.6)9 = ( ? )11 (a) Convert to base 10 using series substitution method: N10 = 1 91 + 8 90 + 6 9-1 = 9 + 8 + 0.666… = (17.666…)10 (b) Convert from base 10 to base 11 using radix divide and multiply method: 7.326 0.666 11 3.586 0.326 11 6.446 0.586 11 N11 = (16.736 …)11

17111110

61

.

Page 17: Number Systems, Base Conversion, and Signed-Numbers …crystal.uta.edu/~carroll/cse2441/uploads/467DEA45-82DF-5992-F58A-E2C01F5FD65C_.pdfBase Conversion (8) • When B = Ak • Algorithm

Base Conversion (8)

• When B = Ak • Algorithm 1.3 (a) To convert a number N from base A to base B when B = Ak and k

is a positive integer, group the digits of N in groups of k digits in both directions from the radix point and then replace each group with the equivalent digit in base B

(b) To convert a number N from base B to base A when B = Ak and k is a positive integer, replace each base B digit in N with the equivalent k digits in base A.

• Examples

– (001 010 111. 100)2 = (127.4)8 (group bits by 3) – (1011 0110 0101 1111)2 = (B65F)16 (group bits by 4)

Page 18: Number Systems, Base Conversion, and Signed-Numbers …crystal.uta.edu/~carroll/cse2441/uploads/467DEA45-82DF-5992-F58A-E2C01F5FD65C_.pdfBase Conversion (8) • When B = Ak • Algorithm

Signed-Number Representation (1)

• Typical formats for integers and fractions

• Signed-number representation • Sign-magnitude • 2’s complement • 1’s complement

Page 19: Number Systems, Base Conversion, and Signed-Numbers …crystal.uta.edu/~carroll/cse2441/uploads/467DEA45-82DF-5992-F58A-E2C01F5FD65C_.pdfBase Conversion (8) • When B = Ak • Algorithm

Signed Number Representation (2)

• Signed Magnitude Method – N = (an-1 ... a0.a-1 ... a-m)r is represented as N = (san-1 ... a0.a-1 ... a-m)rsm, (1.6) where s = 0 if N is positive and s = r -1 otherwise. – N = -(15)10 – In binary: N = -(15)10 = -(1111)2 = (1, 1111)2sm – In decimal: N = -(15)10 = (9, 15)10sm

• Complementary Number Systems – Radix complements (r's complements, 2’s complement for r=2) [N]r = rn - (N)r (1.7) where n is the number of digits in (N)r. – Positive full scale: rn-1 - 1 – Negative full scale: -rn - 1 – Diminished radix complements (r-1’s complements, 1’s complement for r=2)

[N]r-1 = rn - (N)r - 1

Page 20: Number Systems, Base Conversion, and Signed-Numbers …crystal.uta.edu/~carroll/cse2441/uploads/467DEA45-82DF-5992-F58A-E2C01F5FD65C_.pdfBase Conversion (8) • When B = Ak • Algorithm

Signed Binary Numbers (1)

Sign-Magnitude Method – N = (an-1 ... a0)2 is represented as N = (san-1 ... a0.a-1 ... a-m)2sm, where s = 0 if N is positive

and s = 1 if N is negative. – If N = +(1011)2, then N = (01011)2sm – If N = -(1011)2, then N = (11011)2sm

Pros and cons – Simple and easy for humans understand – Complicates computer arithmetic units

• Two representations of zero, +0 and -0 • Need both an adder and a subtractor

Page 21: Number Systems, Base Conversion, and Signed-Numbers …crystal.uta.edu/~carroll/cse2441/uploads/467DEA45-82DF-5992-F58A-E2C01F5FD65C_.pdfBase Conversion (8) • When B = Ak • Algorithm

Signed Binary Numbers (2)

Two’s Complement Method – Let N = (an-1 ... a0)2

– If N ≥ 0, it is represented by (0an-1 ... a0)2

– If N < 0, it is represented by [0an-1 ... A0]2 – 2’s complement [N]2 = 2n - (N)2 , where n is the number of digits in (N)2. – Examples (n = 5, 2n = 100000)

• +(13)10 = +(1101)2 = (01101)2

• -(13)10 = -(1101)2 = [01101]2 = (100000-01101)2 = (10011)2 • +(6)10 = +(0110)2 = (00110)2

• -(6)10 = -(0110)2 = [00110]2 = (100000-00110)2 = (11010)2

• (13)10 – (6)10 = (13)10 + (-(6)10) = (01101)2 + [00110]2 = (01101)2 + (11010)2 = (100111)2 = +(7)10

• (13)10 – (13)10 = (01101)2 + [01101]2 = (01101)2 + (10011)2 = (100000)2 = 0

– Note that 2’s complement is equivalent to bit-by-bit complement + 1.

Page 22: Number Systems, Base Conversion, and Signed-Numbers …crystal.uta.edu/~carroll/cse2441/uploads/467DEA45-82DF-5992-F58A-E2C01F5FD65C_.pdfBase Conversion (8) • When B = Ak • Algorithm

Signed Binary Numbers (3)

One’s Complement Method – Let N = (an-1 ... a0)2

– If N ≥ 0, it is represented by (0an-1 ... a0)2

– If N < 0, it is represented by [0an-1 ... A0]1 – 1’s complement [N]1 = 2n - (N)2 – 1 = [N]2 -1 – Examples (n = 5, 25 = 100000)

• +(13)10 = +(1101)2 = (01101)2

• -(13)10 = -(1101)2 = [01101]1 = (100000-01101-1)2 = (10010)2 • +(6)10 = +(0110)2 = (00110)2

• -(6)10 = -(0110)2 = [00110]1 = (100000-00110-1)2 = (11001)2

• (13)10 – (6)10 = (13)10 + (-(6)10) = (01101)2 + [00110]1 = (01101)2 + (11001)2 = (100110)2 = +(6)10 ??? Must correct using end-around carry.

• (13)10 – (13)10 = (01101)2 + [01101]1 = (01101)2 + (10010)2 = (11111)2 = -0

– Note that 1’s complement is equivalent to bit-by-bit complement.

Page 23: Number Systems, Base Conversion, and Signed-Numbers …crystal.uta.edu/~carroll/cse2441/uploads/467DEA45-82DF-5992-F58A-E2C01F5FD65C_.pdfBase Conversion (8) • When B = Ak • Algorithm

Signed Binary Numbers (4) Decimal Sign-Magnitude Two’s Complement One’s Complement

+7 0111 0111 0111

+6 0110 0110 0110

+5 0101 0101 0101

+4 0100 0100 0100

+3 0011 0011 0011

+2 0010 0010 0010

+1 0001 0001 0001

0 0000, 1000 0000 0000, 1111

-1 1001 1111 1110

-2 1010 1110 1101

-3 1011 1101 1100

-4 1100 1100 1011

-5 1101 1011 1010

-6 1110 1010 1001

-7 1111 1001 1000

-8 NA 1000 NA

Page 24: Number Systems, Base Conversion, and Signed-Numbers …crystal.uta.edu/~carroll/cse2441/uploads/467DEA45-82DF-5992-F58A-E2C01F5FD65C_.pdfBase Conversion (8) • When B = Ak • Algorithm

Two’s Complement Algorithms (1)

• Algorithm 1.4 Find [N]2 given (N)2 . – Copy the bits of N, beginning with the LSD and proceeding

toward the MSD until the first 1 bit is reached. – Leave this 1 as is. – Replace each remaining digit aj , of N by 1 - aj until the MSD has

been replaced,i.e., replace 1 with 0 and 0 with 1.

• Example: 2's complement of (10100)2 is (01100)2. • Example: 2’s complement of N = (10110)2 for n = 8.

– Put three zeros in the MSB position and apply algorithm 1.4 – N = 00010110 – [N]2 = (11101010)2

Page 25: Number Systems, Base Conversion, and Signed-Numbers …crystal.uta.edu/~carroll/cse2441/uploads/467DEA45-82DF-5992-F58A-E2C01F5FD65C_.pdfBase Conversion (8) • When B = Ak • Algorithm

Two’s Complement Algorithms (2)

• Algorithm 1.5 Find [N]2 given (N)2.

First replace each digit, ak , of (N)r by 1 - ak and then add 1 to the resultant.

• This algorithm follows from the equation [N]1 = 2n - (N)2 – 1 = [N]2 -1, i.e.,

[N]2 = [N]1 + 1

• This is equivalent to complementing each digit and then adding 1.

• Example: Find the 2’s complement of N = (10100)2 .

– N = 10100

01011 complement the bits

+1 add 1

[N]2 = 01100

• Example: Find 2’s complement of N = (01100101)2 .

N = 01100101

10011010 Complement the bits

+1 Add 1

[N]2 = (10011011)2

Page 26: Number Systems, Base Conversion, and Signed-Numbers …crystal.uta.edu/~carroll/cse2441/uploads/467DEA45-82DF-5992-F58A-E2C01F5FD65C_.pdfBase Conversion (8) • When B = Ak • Algorithm

Two’s Complement Arithmetic (1)

• Two’s complement number systems are used in computer systems since this reduces hardware requirements (only adders are needed).

• A - B = A + (-B) (add r’s complement of B to A) • Range of numbers in two’s complement number system, where n is the number of

bits.

• 2n-1 -1 = (0, 11 ... 1)2cns and -2n-1 = (1, 00 ... 0)2cns

• If the result of an operation falls outside the range, an overflow condition is said to occur and the result is not valid.

• Consider three cases (where B 0 and C 0):

– A = B + C – A = B - C – A = - B - C

Page 27: Number Systems, Base Conversion, and Signed-Numbers …crystal.uta.edu/~carroll/cse2441/uploads/467DEA45-82DF-5992-F58A-E2C01F5FD65C_.pdfBase Conversion (8) • When B = Ak • Algorithm

Two’s Complement Arithmetic (2)

• Case 1: A = B + C

– (A)2 = (B)2 + (C)2

– If A > 2n-1 -1 (overflow), it is detected by the nth bit, which is set to 1.

– Example: (7)10 + (4)10 = ? using 5-bit two’s complement arithmetic.

• + (7)10 = +(0111)2 = (0, 0111)2cns

• + (4)10 = +(0100)2 = (0, 0100)2cns

• (0, 0111)2cns + (0, 0100)2cns = (0, 1011)2cns = +(1011)2 = +(11)10

• No overflow.

– Example: (9)10 + (8)10 = ?

• + (9)10 = +(1001)2 = (0, 1001)2cns

• + (8)10 = +(1000)2 = (0, 1000)2cns

• (0, 1001)2cns + (0, 1000)2cns = (1, 0001)2cns (overflow)

Page 28: Number Systems, Base Conversion, and Signed-Numbers …crystal.uta.edu/~carroll/cse2441/uploads/467DEA45-82DF-5992-F58A-E2C01F5FD65C_.pdfBase Conversion (8) • When B = Ak • Algorithm

Two’s Complement Arithmetic (3)

• Case 2: A = B - C – A = (B)2 + (-(C)2) = (B)2 + [C]2 = (B)2 + 2n - (C)2 = 2n + (B - C)2 – If B C, then A 2n and the carry is discarded. – So, (A)2 = (B)2 + [C]|carry discarded – If B < C, then A = 2n - (C - B)2 = [C - B]2 or A = -(C - B)2 (no carry in

this case). – No overflow for Case 2. – Example: (14)10 - (9)10 = ?

• Perform (14)10 + (-(9)10) • (14)10 = +(1110)2 = (0, 1110)2cns • -(9)10 = -(1001)2 = (1, 0111)2cns • (14)10 - (9)10 = (0, 1110)2cns + (1, 0111)2cns = (0, 0101)2cns + carry

= +(0101)2 = +(5)10

Page 29: Number Systems, Base Conversion, and Signed-Numbers …crystal.uta.edu/~carroll/cse2441/uploads/467DEA45-82DF-5992-F58A-E2C01F5FD65C_.pdfBase Conversion (8) • When B = Ak • Algorithm

Two’s Complement Arithmetic (4)

– Example: (9)10 - (14)10 = ? • Perform (9)10 + (-(14)10) • (9)10 = +(1001)2 = (0, 1001)2cns • -(14)10 = -(1110)2 = (1, 0010)2cns • (9)10 - (14)10 = (0, 1001)2cns + (1, 0010)2cns = (1, 1011)2cns

= -(0101)2 = -(5)10

– Example: (0, 0100)2cns - (1, 0110)2cns = ?

• Perform (0, 0100)2cns + (- (1, 0110)2cns) • - (1, 0110)2cns = two’s complement of (1,0110)2cns = (0, 1010)2cns • (0, 0100)2cns - (1, 0110)2cns = (0, 0100)2cns + (0, 1010)2cns = (0, 1110)2cns = +(1110)2 = +(14)10 • +(4)10 - (-(10)10) = +(14)10

Page 30: Number Systems, Base Conversion, and Signed-Numbers …crystal.uta.edu/~carroll/cse2441/uploads/467DEA45-82DF-5992-F58A-E2C01F5FD65C_.pdfBase Conversion (8) • When B = Ak • Algorithm

Two’s Complement Arithmetic (5)

• Case 3: A = -B - C – A = [B]2 + [C]2 = 2n - (B)2 + 2n - (C)2 = 2n + 2n - (B + C)2 = 2n + [B + C]2 – The carry bit (2n) is discarded. – An overflow can occur, in which case the sign bit is 0. – Example: -(7)10 - (8)10 = ?

• Perform (-(7)10) + (-(8)10) • -(7)10 = -(0111)2 = (1, 1001)2cns , -(8)10 = -(1000)2 = (1, 1000)2cns • -(7)10 - (8)10 = (1, 1001)2cns + (1, 1000)2cns = (1, 0001)2cns + carry

= -(1111)2 = -(15)10

– Example: -(12)10 - (5)10 = ? • Perform (-(12)10) + (-(5)10) • -(12)10 = -(1100)2 = (1, 0100)2cns , -(5)10 = -(0101)2 = (1, 1011)2cns • -(7)10 - (8)10 = (1, 0100)2cns + (1, 1011)2cns = (0, 1111)2cns + carry • Overflow, because the sign bit is 0.

Page 31: Number Systems, Base Conversion, and Signed-Numbers …crystal.uta.edu/~carroll/cse2441/uploads/467DEA45-82DF-5992-F58A-E2C01F5FD65C_.pdfBase Conversion (8) • When B = Ak • Algorithm

Two’s Complement Arithmetic (6)

• Example: A = (25)10 and B = -(46)10

– A = +(25)10 = (0, 0011001)2cns , -A = (1, 1100111)2cns – B = -(46)10 = -(0, 0101110)2 = (1, 1010010)2cns , -B = (0, 0101110)2cns – A + B = (0, 0011001)2cns + (1, 1010010)2cns = (1, 1101011)2cns = -(21)10 – A - B = A + (-B) = (0, 0011001)2cns + (0, 0101110)2cns = (0, 1000111)2cns = +(71)10 – B - A = B + (-A) = (1, 1010010)2cns + (1, 1100111)2cns = (1, 0111001)2cns + carry = -(0, 1000111)2cns = -(71)10 – -A - B = (-A) + (-B) = (1, 1100111)2cns + (0, 0101110)2cns = (0, 0010101)2cns + carry = +(21)10 – Note: Carry bit is discarded.

Page 32: Number Systems, Base Conversion, and Signed-Numbers …crystal.uta.edu/~carroll/cse2441/uploads/467DEA45-82DF-5992-F58A-E2C01F5FD65C_.pdfBase Conversion (8) • When B = Ak • Algorithm

Two’s Complement Arithmetic (7)

• Summary • When numbers are represented using 2’s complement number system:

– Addition: Add two numbers. – Subtraction: Add two’s complement of the subtrahend to the minuend. – Carry bit is discarded, and overflow is detected as shown above.

Case Carry Sign Bit Condition Overflow ?

B + C 0

0

0

1

B + C 2n-1 - 1

B + C > 2n-1 - 1

No

Yes

B - C 1

0

0

1

B C

B > C

No

No

-B - C 1

1

1

0

-(B + C) -2n-1

-(B + C) < -2n-1No

Yes