ENGIN 241
Digital Systems with Lab
Instructor: Ioannis (Yannis) Zografopoulos
Engineering Department – University of Massachusetts Boston
Two’s complement representation
• Don’t have same problems as sign/magnitude numbers
• Ordinary addition works
• Single representation for 0
• How?
• Assume a number A has N bits
• Most significant bit still indicates the sign
• I.e., 1 = negative, 0 = positive
• But the overall number is interpreted differently from
sign/magnitude
• Most significant bit position has a value of −𝟐𝟐𝑵𝑵−𝟏𝟏
ENGIN 241 Digital Systems with Lab
1
Example (4bit number)
• Let’s assume we have 1101
• MSB = 1 negative number
A = 1101
= a3a2a1a0
= 1*(-23) + (1*22+0*21+1*20)
= -3
+
• Zero is written as all zeros
• -1 is written as all ones: 11…1112
ENGIN 241 Digital Systems with Lab
2
Two’s complement representation
• Assuming that a number A has N bits
• Question: What is the range of N bit two’s complement representation?
• Let’s work with an example
• If N=4
• Most positive 4-bit number 0111= 24−1 − 1
• Most negative 4-bit number 1000= −24−1
• Answer:
• Range of N-bit two’s complement numbers: [−2𝑁𝑁−1 , 2𝑁𝑁−1 − 1]
• There are 2𝑁𝑁 possible values in this representation
ENGIN 241 Digital Systems with Lab
3
Taking the Two’s complement…
• Method to flip the sign of a two’s complement number
• i.e., go from positive to negative, or vice versa
• How?
• Step 1: Invert all the bits
• Step 2: Add 1
• Why is it useful?
1. Find the two’s complement representation of a negative number
2. Find the magnitude of a negative number
ENGIN 241 Digital Systems with Lab
4
Taking the Two’s complement…(examples)
•Why is it useful?
1. Find the two’s complement representation of a
negative number
• E.g.,
Find the two’s complement of -3
• Start with
310 = 00112
• Step 1: Invert all bits 11002
• Step 2: Add 1
+
12
• Result:
11012 = −310
ENGIN 241 Digital Systems with Lab
5
Taking the Two’s complement…(examples cntd’)
• Why is it useful?
2. Find the magnitude of a negative number
• E.g., Find the decimal value of the two’s complement number 𝟏𝟏𝟏𝟏𝟏𝟏𝟏𝟏𝟐𝟐 ?
• Step 1: Invert the bits of 𝟏𝟏𝟏𝟏𝟏𝟏𝟏𝟏𝟐𝟐
01102
• Step 2: Add 1
+
12
• Result:
01112
• Thus, 𝟏𝟏𝟏𝟏𝟏𝟏𝟏𝟏𝟐𝟐 = −𝟕𝟕𝟏𝟏𝟏𝟏
ENGIN 241 Digital Systems with Lab
which is 710
6
Advantages of two’s complement
• Ordinary addition works properly for both positive and
negative numbers
• Contrary to sign/magnitude representation
• Note that for an N-bit number, the carry out of the (N-1)-th bit
is discarded when adding N-bit numbers and
• This carry is NOT considered as overflow!
• See following example
ENGIN 241 Digital Systems with Lab
7
Two’s Complement – Addition
• Add 6 + (-6) using two’s complement numbers
0110
These are NOT considered
+ 1010
overflow bits!!!
10000
• Add -2 + 3 using two’s complement numbers
1110
+ 0011
10001
ENGIN 241 Digital Systems with Lab
8
Two’s Complement – Subtraction
• Given two numbers A and B
• Perform the A-B Subtraction
• How?
• Step 1: Take the two’s complement of the second term
• i.e., B in our case
• Step 2: Add to the first term
• i.e., A in our case
• Essentially, instead of doing A-B we do A+(-B)
ENGIN 241 Digital Systems with Lab
9
Two’s Complement – Subtraction (examples)
• Example 1: 5 – 3
A = 5 = 0101
B = 3 = 0011
-B = -3 = 1100 + 1 = 1101
0101
+ 1101
10010 = 210
ENGIN 241 Digital Systems with Lab
10
Two’s Complement – Subtraction (examples)
• Example 2: – 5 – 3
• A = -5
5 = 0101
1010
+
1
1011
• B = 3 = 0011
-B = -3 = 1100 + 1 = 1101
ENGIN 241 Digital Systems with Lab
• A + (-B)
1011
+ 1101
11000 = −𝟖𝟖𝟏𝟏𝟏𝟏
11
Overflow detection – two’s complement Addition
• Unlike unsigned numbers, a carry out at the MSB is not overflow!
• Adding a negative and a positive number never creates an overflow
(why?)
• Overflow happens when adding two numbers that:
• Have the same sign
• The result of their summation
has the opposite sign
* Blue circles indicate the sign bits
ENGIN 241 Digital Systems with Lab
12
Overflow detection – two’s complement Subtraction
• Assuming A-B
• Overflow happens when the two inputs have different signs
and
• The subtraction result sign has a different sign from input A
ENGIN 241 Digital Systems with Lab
13
Overflow detection – Rule of thumb
• In general, when performing addition A+B or subtraction A-B
• Overflow can happen when:
1. A and B have same signs upon addition
OR
2. A and B have different signs upon subtraction
AND
A and the result have opposite signs
ENGIN 241 Digital Systems with Lab
14
Increasing bit width
• What happens if we have overflow?
• Extend representation from N-bit to M-bit, where M>N
• How?
• For two’s complement numbers, use the sign-extension
method
• Sign bit copied to higher significant bits
• Number value remains the same
ENGIN 241 Digital Systems with Lab
15
Sign Extension Examples
• 4-bit representation of 310 = 00112
• 8-bit representation of 310 = 0000 00112
• 16-bit representation of 310 = 0000 0000 0000 00112 = 0x0003
• ….
• 4-bit representation of −510 = 10112
• 8-bit representation of −510 = 1111 10112
• 16-bit representation of −510 = 1111 1111 1111 10112 = 0xfffb
• ….
ENGIN 241 Digital Systems with Lab
16
Summary number systems range
• For any N-bit system (Why?)
• Unless stated otherwise, all signed
binary numbers use two’s
complement representation
Number System
Range
Unsigned
[0, 2N-1]
Sign/Magnitude
[-(2N-1-1), 2N-1-1]
Two’s Complement
[-2N-1, 2N-1-1]
• Example: 4-bit system
-8
-7
-6
-5
-4
-3
-2
-1
Unsigned
0
1
2
3
4
5
6
7
ENGIN 241 Digital Systems with Lab
9
10
11
12
13
14
15
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
1000 1001 1010 1011 1100 1101 1110 1111 0000 0001 0010 0011 0100 0101 0110 0111
1111 1110 1101 1100 1011 1010 1001
8
0000
0001 0010 0011 0100 0101 0110 0111
1000
Two's Complement
Sign/Magnitude
17
Book Chapters
• This lecture covers material from Chapter 1 of the book
• Digital Systems
• Digital Abstraction
• Basics of Binary Systems
• Logic Gates
• Logic Levels
ENGIN 241 Digital Systems with Lab
18