Complement

9
One’s and Two’s Complement Numbers and Arithmetic Operations 1 Digital electronics 13IDP14

description

1's and 2's complements

Transcript of Complement

Page 1: Complement

One’s and Two’s Complement Numbers and

Arithmetic Operations

1

Digital electronics13IDP14

Page 2: Complement

2

OutlineOutline Number of binary digits (bits) used in

computers for arithmetic operations Positive and negative number

representation using bits 1’s complement and 2’s complement Arithmetic operations in 2’s complement Carry and overflow concepts See module 2a notes for more examples

Page 3: Complement

Bits Used in Computers for Numbers and Implications 4, 8, 16, 32, 64 Using 4 bits we can only represent 24 = 16

numbers -8, -7, -6, -5, …-1, 0, 1, … 7 Using 8 bits we can only represent 28 = 256

numbers -128, -127, …-1, 0, 1, … 127 Since we can represent only a fixed number of

positive and negative numbers, we may get wrong results when we compute arithmetic operations

We must determine when the results are wrong

Page 4: Complement

One’s and Two’s Complement

0111

0110

0101

0100

0011

0010

0001

0000

1111

1110

1101

1100

1011 1010 1001 1000

+7 +6 +5 +4 +3 +2 +1 +0 -0 -1 -2 -3 -4 -5 -6 -7

Cryptography -Part -I 4

0111

0110

0101

0100

0011

0010

0001

0000

1111

1110

1101

1100

1011

1010

1001

1000

+7 +6 +5 +4 +3 +2 +1 +0 -1 -2 -3 -4 -5 -6 -7 -8

One’s Complement

Two’s Complement

Page 5: Complement

Two’s Complement Number Representation and Value (1) MSB represents negative number with a

value. Example: 8-bit number say 1101 0011 Value in decimal = -1*27 + 1*26 + 0*25 + 1*24 + 0*23 +0*22 +

1*21 + 1 =-128 +64+0+16+0+0+2+1 =-128+83 =-45

Cryptography -Part -I 5

Page 6: Complement

Two’s Complement Number Representation and Value (2) Example: 1111 1111 Decimal Value = -1*27 + 1*26 + 1*25 + 1*24 + 1*23 +1*22 +

1*21 + 1 = -128+64+32+16+8+4+2+1 =-128+127 = -

1 Example: 1000 0000 Value = -128 Example: 0111 1111 Value = 127

Cryptography -Part -I 6

Page 7: Complement

Finding a Negative of a number Assume 1’s complement Number 8-bit: 0000 1111; value =15 Given 15 determine -15 Change all 0 to 1 and 1 to zero Result: 1111 0000 = -127+64+32+16 =-127+112 = -

15 Assume 2’s complement Number 8-bit: 0000 1111; value =15 Given 15 determine -15 Change all 0 to 1 and 1 to zero then add 1 Result: 1111 0001 = -128+64+32+16+1 =-128+113 =

-157

Page 8: Complement

Arithmetic Operations in Two’s Complement (1) Example:Find 7 – 5 assuming 4-bit

numbers 7 = 0111 ; -5 =1011 in 2’s complement

0111 1011 -----10010

Carry in MSB 1, Carry out in MSB 1Result ok value 2

Page 9: Complement

Arithmetic Operations in Two’s Complement (2) Example:Find 7 + 5 assuming 4-bit

numbers 7 = 0111 ; 5 =0101 in 2’s complement

0111 0101 -----1100

Carry in MSB 1, Carry out in MSB 0Result wrong; value -4. We can not represent

real value 12. This is overflow.

9