Department of Electrical, Electronics and Communication Engineering
# Lecture 5
Signed Number System
Unsigned Magnitude
Data Representation Sign Magnitude
1’s Complement
2’s Complement
1’s compliment
1111 = 15-- 1111
0111 = 7--- 1000=-
7
1’s compliment
1111
0111 = 7--- 1000=-
7
2’s compliment
10000
0111 = 1000 + 1 = 1001
1001
Goal of negative number systems
• Signed system: Simple. Just flip the sign bit
• 0 = positive
• 1 = negative
• One’s complement: Replace subtraction with addition
– Easy to derive (Just flip every bit)
• Two’s complement: Replace subtraction with addition
– Addition of one’s complement and one produces
the two’s complement.
5
Definitions: Given a positive integer x, we represent -x
• 1’s complement:
Formula: 2n -1 – x
• i.e. n=4, 24 – 1 – x = 15 – x
• In binary: (1 1 1 1) – (b3 b2 b1 b0)
• Just flip all the bits.
• 2’s complement:
Formula: 2n –x
• i.e. n=4, 24 – x = 16 – x
• In binary: (1 0 0 0 0) – (0 b3 b2 b1 b0)
• Just flip all the bits and add 1.
6
Definitions:
4-Bit Example
7
Definitions: Examples
Given n-bits, what is the range of my numbers in each system?
• 3 bits: • 5 bits:
– Signed: -3 , 3 – Signed: -15, 15
– 1’s: -3 , 3 – 1’s: -15, 15
– 2’s: -4 , 3 – 2’s: -16, 15
• 6 bits • Given 8 bits
– Signed: -31, 31 – Signed: -127, 127
– 1’s: -31, 31 – 1’s: -127, 127
– 2’s: -32, 31 – 2’s: -128, 127
Formula for calculating Signed & 1’s: -(2n-1 – 1) , (2n-1 – 1)
the range 2’s: -2n-1 , (2n-1 – 1)
8
Arithmetic Operations:
Derivation of 1’s Complement
Theorem 1: For 1’s complement, given a positive
number ( xn-1 , xn-2 , … , x0 ), the negative number is
( ) where
Proof:
(i). 2n-1 in binary is an n bit vector (1,1, …, 1)
(ii). 2n-1-x in binary is (1,1, …,1) – (xn-1,xn-2, …, x0).
The result is
( )
9
Arithmetic Operations: Derivation of 2’s Complement
Theorem 2: For 2’s complement, given a positive
integer x, the negative number is the sum of its 1’s
complement and 1.
Proof: 2n – x = 2n – 1 – x + 1. Thus, the 2’s
complement is
( ) + (0, 0, …, 1)
Ex: x = 9 (01001) Ex: x = 13 (01101)
1’s -9 (10110) 1’s -13 (10010)
31– 9 = 22 31-13=18
2’s -9 (10111) 2’s -13 (10011)
32 – 9 = 23 32-13=19
10
One’s Complement Hardware:
xn-1 xn-2 … x0
Inverters
xn-1 xn-2 … x0
11
Arithmetic Operations: 2’s Complement
Input: two positive integers x & y,
1. We represent the operands in two’s complement.
2. We sum up the two operands and ignore bit n.
3. The result is the solution in two’s complement.
Arithmetic 2’s complement
x+y x+y
x-y x + (2n - y) = 2n+(x-y)
-x + y (2n - x) + y =2n+(-x+y)
-x - y (2n - x) + (2n - y) = 2n+2n-x-y
Arithmetic Operations: Example: 4 – 3 = 1
410 = 01002
310 = 00112 -310 11012
0100
+ 1101
10001 1 (after discarding extra bit)
We discard the extra 1 at the left which is 2n from 2’s
complement of -3. Note that bit bn-1 is 0. Thus, the result
is positive.
13
Arithmetic Operations: Example: -4 +3 = -1
410 = 01002 -410 Using two’s comp. 1011 + 1 = 11002
(Invert bits)
310 = 00112
1100
+ 0011
1111 Using two’s comp. 0000 + 1 = 1, so our answer is -1
If left-most bit is 1, it means that we have a negative
number.
14
Arithmetic Operations: 1’s Complement
Input: two positive integers x & y,
1. We represent the operands in one’s complement.
2. We sum up the two operands.
3. We delete 2n-1 if there is carry out at left.
4. The result is the solution in one’s complement.
Arithmetic 1’s complement
x+y x+y
x-y x + (2n -1- y) = 2n-1+(x-y)
-x + y (2n -1-x) + y =2n-1+(-x+y)
-x - y (2n -1-x) + (2n -1-y) = 2n-1+(2n-1-x-y)
Arithmetic Operations: Example: 4 – 3 = 1
410 = 01002
310 = 00112 -310 11002 in one’s complement
0100 (4 in decimal )
+ 1100 (12 in decimal or 15-3 )
1,0000 (16 in decimal or 15+1 )
0001(after deleting 2n-1)
We discard the extra 1 at the left which is 2n and add one
at the first bit.
16
Arithmetic Operations: Example: -4 +3 = -1
410 = 01002 -410 Using one’s comp. 10112
(Invert bits)
310 = 00112
1011 ( 11 in decimal or 15-4 )
+ 0011 ( 3 in decimal )
1110 ( 14 in decimal or 15-1 )
If the left-most bit is 1, it means that we have a negative
number.
17
One’s Complement
Example: – 4 – 3 = – 7
4 in binary = 0100.
Flipping the bits, you get –4 (1011) in binary.
3 in binary = 0011.
Flipping the bits, you get –3 (1100) in binary.
1011 (11 in decimal, or 15-4)
+ 1100 (12 in decimal, or 15-3)
-----------------------------------------
1,0111 (23 in decimal (15+15-7))
So now take the extra 1 and remove it from the 5th
spot and add it to the remainder
0111
+ 1
------------------------------------------
1000 (-7 in 1’s comp) 18
THANK YOU