CH 1 Number Systems and Conversions - 3 NR

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Introduction,

Number systems and Conversion (C)

EE & IT AU
Hasan Zidan

1
One’s Complement Representation
One’s complement is another way to represent signed integers.

To encode a negative number, get the binary representation of


its magnitude, then COMPLEMENT each bit. Complementing
each bit means replacing 1 by 0 and 0 by 1.
What is - 5 in One’s Complement, 8 bits?
The magnitude 5 in 8-bits is 0000 0101
Now complement each bit: 1111 1010 is the 8-bit, one’s
complement of -5.

NOTE: positive numbers in 1’s complement


are simply their binary representation.

2
One’s Complement Examples
-5 = 1111 1010
+5 = 0000 0101
+127 = 0111 1111
-127 = 1000 0000
+0 = 0000 0000
-0 = 1111 1111

For 8 bits, can represent the signed integers -255 to +255.


For N bits, can represent the signed integers

-(2N – 1) to + (2N – 1)

3
One’s Complement (cont.)
n-1
1. Addition of two positive no’s., sum < 2 (n=8)
+25 0001 1001
+
+14 0000 1110 Correct answer
+39 0010 0111
n-1
2. Addition of two positive no’s., sum >= 2
+125 0111 1101
+
+ 14 0000 1110 Wrong answer (-) overflow
-116 1000 1011
3. Addition of +ve < –ve in magnitude
+25 0001 1001
+ Correct answer
- 40 1101 0111
-15 1111 0000 4
4. Addition of +ve > –ve in magnitude
+ - 25 1110 0110
Correct answer no overflow
+ 40 0010 1000
+ 15 (1) 0000 1110
1 End-around carry
0000 1111
n-1
5. Addition of two -ve no’s., |sum| < 2
- 25 1110 0110
+
- 14 1111 0001 Correct answer no overflow
- 39 (1) 1101 0111
1
1101 1000
n-1
6. Addition of two negative no’s., |sum| >= 2
-125 1000 0010
+ 1111 0001
- 14 Wrong answer (+) overflow
- 139 (1) 0111 0011
1
0111 0100
One’s Complement Comments

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.
(Problem No. 1)

However, addition of K + (-K) now gives Zero!

-5 + 5 = -0 (Problem No. 2)

6
Two’s Complement Representation

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. (get
One’s complement, then add 1).
What is -5 in Two’s Complement, 8 bits?
The magnitude 5 in 8-bits is 0000 0101
Now complement each bit: 1111 1010
Now add one: 1111 1011
is the 8-bit, two’s complement representation of -5.

NOTE: positive numbers in 2’s complement are


simply their binary representation.
7
Two’s Complement Examples
-5 = 1111 1011
+5 = 0000 0101
+127 = 0111 1111
-127 = 1000 0001
-128 = 1000 0000 (note the extended range)
+0 = 0000 0000
-0 = 0000 0000 (only one zero)
For 8 bits, can represent the signed integers -128 to +127.
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.
8
Two’s Complement (cont.)
n-1
1. Addition of two positive no’s., sum < 2 (n=8)
+25 0001 1001
+
+14 0000 1110 Correct answer
+39 0010 0111
n-1
2. Addition of two positive no’s., sum >= 2
+125 0111 1101
+
+ 14 0000 1110 Wrong answer (-) overflow
-117 1000 1011
3. Addition of +ve < –ve in magnitude

+ +25 0001 1001


- 40 1101 1000 Correct answer
-15 1111 0001 9
Two’s Complement (cont.)
4. Addition of +ve > –ve in magnitude
- 25 1110 0111
+
+ 40 0010 1000 Correct answer no overflow
+ 15 1 0000 1111
n-1
5. Addition of two -ve no’s., |sum| <= 2
- 25 1110 0111
+
- 14 1111 0010 Correct answer no overflow
- 39 1 1101 1001
n-1
6. Addition of two negative no’s., |sum| > 2
-125 1000 0011
+ - 14 Wrong answer (+) overflow
1111 0010
117 1 0111 0101
10
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 One’s


Complement.

There is only one zero, and K + (-K) = 0. (Problem No. 1)


-5 + 5 = 0
Normal binary addition is used for adding numbers that
represent two’s complement integers.

11
What do you need to know?

• Number ranges for unsigned, Signed Magnitude, 1st


complement and 2nd complement.

• Addition and subtraction in SMMM, 1st and 2nd


complement

12

You might also like