Representation of Negative Numbers and
Representation of Negative Numbers and
18B11EC215
Lecture3
1
Outline
■ Unsigned Binary Numbers
■ Signed Magnitude Representation
■ Diminished Radix/One’s complement Representation/r-1’s
■ Radix/Two’s Complement Representation/r’s
2
UNSIGNED BINARY NUMBERS
■ In some applications, data is either positive or negative.
■ Data is called unsigned binary because all bits are used to represent magnitude
of the corresponding decimal only.
3
Signed Integer Representation[2]
■ We have been ignoring signed integers.
■ The PROBLEM with signed integers ( - 45, + 27, -99) is the SIGN!
■ How do we encode the sign?
■ The sign is an extra piece of information that has to be encoded in addition to
the magnitude.
4
What can we do?[1,2]
Following are the three possible techniques
5
Signed Magnitude Representation[2]
■ Signed Magnitude (SM) is a method for encoding signed integers.
■ The Most Significant Bit (MSB) is used to represent the sign.
■ ‘1’ is used for a ‘-’ (negative sign),
■ ‘0’ for a ‘+’ (positive sign).
6
Format of SM Representation[2]
7
Signed Magnitude Ex. (8 bits)[2]
■ -5 = (1 0000101)2 = (85)16
■ +5 = (0 0000101)2 = (05)16
■ +127 = (0 1111111)2 = (7F)16
■ -127 = (1 1111111)2 = (FF)16
■ +0 = (0 0000000)2 = (00)16
■ -0 = (1 0000000)2 = (80)16
8
■ For N bits, can represent the signed integers
- {2(N-1) – 1} to {+ 2(N-1) – 1}
■ For 8 bits, can represent the signed integers -127 to +127.
■ Signed magnitude is easy to understand and encode.
■ Simple.
■ Negative numbers are identical to positive numbers except the sign bit.
■ Used today in some applications.
9
Problems with Signed Magnitude
Representation[2]
10
One’s Complement Representation[2]
11
Example[2]
■ How is -5 represented in One’s Complement (encoded in 8 bits) ?
The magnitude 5 in 8-bits is
( 00000101)2 = (05)16
■ Now complement each bit:
(11111010)2 = (FA)16
■ (FA)16 is one’s complement representation of -5.
12
One’s Complement Examples
■ +5 = (00000101)2 = ( 05 )16
■ -5 = (11111010) = ( FA )16
■ +127 = (01111111)2 = ( 7F )16
■ -127 = (10000000)2 = ( 80 )16
■ +0 = (00000000)2 = ( 00 )16
■ -0 = (11111111)2 = ( FF )16
13
■ For N bits, can represent the signed integers
- {2(N-1) – 1} to + {2(N-1) – 1}
■ For 8 bits, can represent the signed integers -127 to +127.
14
Limitations of One’s Complement Method
■ Still have the problem that there are two ways of representing 0 (-0, and +0)
■ Mathematically speaking, no such thing as two representations for zeros.
■ However, addition of K + (-K) now gives Zero!
-5 + 5 = ( FA )16 + ( 05 )16
= ( FF)16
= -0 !!!
15
■ Unfortunately, K + 0 = K only works
if we use +0,
■ Does not work if we use -0.
■ 5 + (+0) = (05)16 + (00)16 = (05)16 = 5 (ok)
■ 5 + (-0) = (05)16 + (FF)16= (04)16 = 4 (wrong)
16
Two’s Complement Representation[2]
■ Two’s complement is another way to represent signed integers.
■ To encode a negative number, get the binary representation of its magnitude,
COMPLEMENT each bit, then ADD 1.
or
■ See 1 from LSB , mark it and complement all bits which are occurring before it.
17
Example[2]
■ What is -5 in Two’s Complement, 8 bits?
(00000101)2 = (05)16
■ Now complement each bit:
(11111010)2 = (FA)16
■ Now add one: (FA) 16 + 1 = (FB)16
18
Two’s Complement Examples
■ -5 = ( 11111011) = ( FB)16
■ +5 = ( 00000101 )= ( 05)16
19
■ For N bits, can represent the signed integers
-2(N-1) to + 2(N-1) - 1
■ Note that negative range extends one more than positive range.
■ For 8 bits, can represent the signed integers -128 to +127.
20
Two’s Complement Comments
■ Two’s complement is the method of choice for representing signed integers.
■ It has none of the drawbacks of Signed Magnitude or Ones Complement.
■ There is only one zero, and K + (-K) = 0.
-5 + 5 = (FB)16 + ( 05)16 = (00)16 = 0 !!!
■ Normal binary addition is used for adding numbers that represent two’s complement
integers.
21
Complements[3]
■ Complements are used in digital computers for simplifying the subtraction operation
and for logical manipulation.
(2) The (r-1)’s complement. E.g. 1’s complement for binary and 9’s complement for
decimal.
22
The r’s complement[3]
and with n number of digits
■ Given a positive number N in base r with and integer part of n digits, the r’s
complement of N is defined as :
(rn) – N for N ≠ 0
0 for N=0
23
Examples
■ 10’s complement of (36360)10 = 105 – 36360
Note: Here n= 5
Ans : 63640
Note: Here n= 0
Ans : 0.6466
Note: Here n= 2
Ans : 74.361
24
Examples[1]
■ The 2’s complement of (101100)2
= (26)10 - (101100)2= (1000000-101100)2=010100
Note: In general the 2’s complement can be formed by leaving all significant zeros
and the first nonzero digit unchanged, and then replacing 1’s by 0’s and 0’s by
1’s in all other higher significant digits.
25
References
[1] Reshu Gupta, Amit Gupta ,Atul Kumar Sharma “ Switching Theory(Digital
Electronics)”, Tech India Publication Series, Satya Prakashan, New Delhi.
[2] R. P. Jain, “Modern Digital Electronics,” 4th Edition, Tata McGraw-Hill Education,
2009.
[3] M. Morris Mano and Michael D. Ciletti, “Digital Design with an Introduction to the
Verilog HDL,” 5th Edition, Pearson Education,2013.
26