Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 22

Chapter 3

Complements

 to simplify subtraction operation


 for logical manipulation

2 types of complements for base ‘ r’ system: r’s complement and (r-1)’s complement

( r - l )'s Complement
Given a number N in base r having n digits, the (r - 1)'s complement of N is
defined as (rn - 1) - N.

r’ s complement

The r's complement of an n-digit number N in base r is defined as rn - N OR [(rn – 1)-N]+1


for N ≠ 0 and 0 for N = 0.
[(rn – 1)-N]+1 implies (r-1)’s complement + 1

Example 1:
Decimal number system: r=10. So (r-1)=9. Two types of complements are 9’s and 10’s
complements.

Let N=546700 in decimal number system. Find 9’s and 10’s complement.

Given N=546700, n=6, r=10


9’s complement=(106 -1)-546700=(1000000 – 1) – 546700 = 999999 – 546700= 453299
10’s complement=106 – 546700= 1000000 – 546700 = 453300 OR
9’s complement + 1= 453300

Example 2:
Binary number system: r=2. So (r-1)=1. Two types of complements are 1’s and 2’s
complements.

Let N=1011001 in binary number system. Find 1’s and 2’s complement.

Given N=1011001, n=7, r=2


1’s complement=(27 -1)-1011001=(10000000 – 1) – 1011001 = 1111111 – 1011001= 0100110
2’s complement=27 – 1011001= 10000000 – 1011001 = 00100110 OR
1’s complement + 1= 0100111

Binary addition
A B Sum=A+B Carry
0 0 0 0
0 1 1 0
1 0 1 0
1 1 0 1
Binary Subtraction

A B Sub=A-B Borro
w
0 0 0 0
0 1 1 1
1 0 1 0
1 1 0 0

0-1=> If we take a borrow(1) from the previous bit, then 10-1=1 in binary (10=2 in decimal,1 is 1
in decimal, 2-1=1 of decimal=1 in binary)
1’s complement of a number N can be obtained using 2 methods:
(a) Subtract each bit of N from 1.
N=1011001. 1’s complement: 0100110
(b) Interchange 0’s and 1’s bits of N.
N=1011001. 1’s complement: 0100110

2’s complement of a number N can be obtained by computing 1’s complement of N and adding 1 to it.

Unsigned number: positive number

Subtraction of unsigned numbers (M-N)


1. Find r’s complement of N and add it to M
2. If M>=N, the sum will result in end carry, discard it.
3. If M<N, then sum will not result in end carry. Find r’s complement of the result and attach a
negative sign to the answer.
Compute 72532 – 13250 where r=10
Compute 13250 – 72532 where r=10

Integer representation
2 types of integer values: unsigned (all positive integers), signed (positive and negative numbers)
Signed integers: MSB=0 (+ve positive integer), MSB=1(-ve integer)

Let the binary number be 0101111 consisting of N=7 bits. Here Least Significant Bit
(LSB)=1 placed at 0th position. Most Significant Bit (MSB)=0 placed at 6th position.

Positive number has sign bit=0 at MSB and negative number has sign bit=1 at MSB.

Signed integer has 2 parts (sign bit, Magnitude)

Positive integer has only one representation.


Negative integer has 3 representations
Let the number be stored in 8 bits.

14=1110 binary

+14 0 0001110
-14 has 3 representations:
signed magnitude form : complement sign bit of +14=1 0001110
1’s complement form: find 1’s complement of +14 including sign bit = 11110001
2’s complement form: find 2’s complement of +14 including sign bit = 11110010

Store +0 and -0(all 3 representations) in 8 bit register.

+0= 0 0000000
-0 has 3 representations
 signed magnitude form:1 0000000
 1’s complement form: 1 1111111
 2’s complement form: 1 00000000 =00000000
Since the number is stored in 8 bits, therefore 1 will be ignored. 2’s complement of -0=00000000
+0 and -0 have same representations in 2’s complement whereas they have different representations in
1’s complement and sign magnitude form.

2’s complement +N= -N in 2’s complement representation


2’s complement -N= +N
Example: 2’s complement of +5 = (-5)
2’s complement of -5=+5

Arithmetic Addition

 Represent negative number in 2’s complement form

 Add the two numbers

 If end carry then discard

 If no end carry then answer is negative and in 2’s complement

form

Example: Store numbers in 8 bits and compute:

+5 +( +7)
+5 + (-7)
-5 + (+7)
-5 + (-7)
+5= 00000101

+7=00000111

-5 in 2’s complement form=11111011

-7 in 2’s complement form=11111001

Solve +5 + (-7)

+5=00000101

-7=11111001

+5+(-7)=11111110= -2 in 2’s complement form


To verify:
2 solutions:
Solution 1
Find 2’s complement of -2(result)=+2( 00000001+1=00000010)
Solution 2
Find 2’s complement of +2=-2(result)
+2=00000010
-2 in 2’s complement form=11111101+1=11111110

Solve -5 + (+7)

-5=11111011

+7=00000111

-5+(+7)=00000010

Solve -5 + (-7)

-5=11111011
-7=11111001

-5+(-7)=11110100= -12 in 2’s complement form


To verify: Find 2’s complement of +12 (00001011+1=00001100)

Note: 2’s complement of a binary number +N= -N and similarly 2’s complement of a binary
number

-N= +N

Arithmetic Subtraction
Subtraction of two signed binary numbers when negative numbers are in 2's complement
form can be stated as follows:
 Take the 2's complement of the subtrahend B (including the sign bit)
 Add it to minuend A (including the sign bit).
 If carry then discard
 If no carry then answer is negative and in 2’s complement form

Arithmetic subtraction can be represented in the form of addition as follows:

+A – (+B) = +A + (-B)

+A – (-B)= +A + (+B)

-A –(+B)= -A + (-B)

-A –(-B) = -A + (+B)

Overflow

In CPU, there is a status register which stores values such as carry bit, sign bit, overflow
bit etc.
 When two unsigned numbers are added, an overflow is detected from the end carry
out of the most significant position.
 When two signed numbers are added, the sign bit is treated as part of the number
and the end carry does not indicate an overflow.
 An overflow may occur if the two numbers added are both positive or both negative.
Overflow detection
If the carry into sign bit and carry out of sign bit are not equal, then overflow
occurs.
8 bit register: range +127 to -128 (0 to 127 & -1 to -128)

BCD addition

Arithmetic operations:

 Decimal to BCD conversion,

 perform arithmetic operation

 BCD to decimal conversion

Disadvantages:
 Wastage of considerable amount of storage space since the number of bits needed to
store a decimal number in a binary code is greater than the number of bits needed
for its equivalent binary representation.
 the circuits required to perform decimal arithmetic are more complex.

Advantages
 Some applications, such as business data processing, require small amounts of
arithmetic computations compared to the amount required for input and output of
decimal data.
 eliminates the need for conversion to binary and back to decimal.

Plus sign is represented by 0 i.e. 0000


Minus sign is represented by 9 i.e. 1001

BCD addition

All negative numbers are represented in 10’s complement form.


While adding 2 BCD numbers, if the addition of 2 digits results in number between
1010,1011,1100,1101,1110,1111 (i.e. 10 to 15) which is not a valid BCD OR there is
a carry into next digit then 0110 (6) is added to the current digit.
Radix/base
3-6. What is the radix of the numbers if the solution to the quadratic equation
x2 – 10x + 31 = 0 is x = 5 and x = 8?

Method 1: Let the radix be r.


x2 – 10x + 31=0
(x-5)(x-8)=0

(x2 – 10x + 31)r= [(x-5)(x-8)]10


(x2 – 10x + 31)r= (x2-13x+40)10
(X2)r=(x2)10
(10)r=(13)10=1*r +0*r =1*10 +3*10 = r =13
1 0 1 0

(31)r=(40)10
Verify: (31)13=(40)10

Method 2 : Let the radix be r.


Given x=5 and x=8
52 – 10*5 + 31= 82 – 10*8 + 31
(5*r0)2 – (1*r1 + 0*r0)*5 + 3*r1 + 1*r0 = (8*r0)2 – (1*r1 + 0*r0)*8 + 3*r1 + 1*r0
25 – 5r + 3r + 1 = 64 – 8r + 3r +1
25 – 5r = 64 – 8r
r = 39/3=13

Question
Find the largest and smallest binary integers that can be expressed with N bits in the
following representations:
(a) unsigned representation
(b) signed magnitude representation
(c) signed 1’s complement representation
(d) signed 2’s complement representation

largest smallest
unsigned 2N - 1 0
signed magnitude +2 (N-1) - 1 -(2 (N-1) – 1)
signed 1’s complement +2 (N-1) - 1 -(2 (N-1) – 1)
signed 2’s complement +2 (N-1) - 1 -2 (N-1)

Examples:

largest smallest
N=2 N=4 N=8 N=2 N=4 N=8
unsigned 3 15 255 0 0 0
signed magnitude +1 +7 +127 -1 -7 -127
signed 1’s complement +1 +7 +127 -1 -7 -127
signed 2’s complement +1 +7 +127 -2 -8 -128
Range of values in binary number system

Unsigned : N=2 (0,1,2,3)


N=4 (0,1,2,…….,15)
N=8 (0,1,2,….....,255)

Signed magnitude, signed 1’s complement


N=2 (-1, -0, +0, +1)
N=4 (-7, -6, ….-0, +0, +1,…....,+7)
N=8 (-127, -126, ……, -0, +0, +1, +2,….....,+127)

Signed 2’s complement


N=2 (-2, -1, +0, +1)
N=4 (-8,-7, -6, ….-1, +0, +1,…....,+7)
N=8 (-128, -127, ……, -1, +0, +1, +2,….....,+127)

Largest and smallest values in binary

Unsigned : N=2 (largest=11, smallest=00)


N=4 (largest=1111, smallest=0000)
N=8 (largest=11111111, smallest=00000000)

Signed magnitude (bit at MSB represents positive or negative)


N=2 (largest=0 1, smallest=1 1)
N=4 (largest=0 111, smallest=1 111)
N=8 (largest=0 1111111, smallest=1 1111111)

Signed 1’s complement


N=2 (largest=0 1, smallest=1 0)
N=4 (largest=0 111, smallest=1 000)
N=8 (largest=0 1111111, smallest=1 0000000)

Signed 2’s complement


N=2 (largest=0 1, smallest=1 0)
N=4 (largest=0 111, smallest=1 000)
N=8 (largest=0 1111111, smallest=1 0000000)

Signed 1’s complement:


-1 = 10
-7=1000
-127=10000000

Signed 2’s complement:


-2 = 10
-8=1000
-128=10000000
Use 8 bit register
Find 1’s complement of +127
Find 2’s complement of +128
Real number representation

In addition to the sign, a number may have a binary (or decimal) point. The position of the
binary point is needed to represent fractions, integers, or mixed integer-fraction numbers.
There are two ways of specifying the position of the binary point in a register: by giving it a
fixed position or by employing a floating-point representation.

Two methods: fixed point and floating point representation

The fixed-point method assumes that the binary point is always fixed in one position. The
two positions most widely used are
(1) a binary point in the extreme left of the register to make the stored number a
fraction
(2) a binary point in the extreme right of the register to make the stored number an
integer.

In either case, the binary point is not actually present, but its presence is assumed from the
fact that the number stored in the register is treated as a fraction or as an integer.

Example: 11.10 =0 .1110x22=0.1110x100=0 .111000

256.75=25675x10-2
256.75=.25675x103

The floating-point representation uses a second register to store a number that designates
the position of the decimal point in the first register.
+1001.11

8 bits fraction and 6 bits exponent


+1001.11=+0.100111x2+4
Mantissa=+.100111, exponent=+4
8 bits fraction= 1sign bit, 7 bits fraction =0,1001110=01001110
6 bits exponent =1 sign bit, 5 bits exponent=0,00100=000100

Two main standard forms of floating point numbers are from IEEE (Institute of Electrical
and Electronic Engineers) and ANSI (American National Standards Institute)
organizations.

ANSI 32-bit floating-point numbers in byte format:

Byte1 : SEEEEEEE
S: sign bit of Mantissa
E: exponent bits (negative power is represented in 2’s complement form)
Byte2, Byte3, Byte4 for mantissa
Mantissa is in signed magnitude form.

Format is
SEEEEEEE MMMMMMMM MMMMMMMM MMMMMMMM

assumed binary point

examples
(a) 13=(1101)2 = 0.1101 x 2 +4

00000100 11010000 00000000 00000000

(b) -13= -(1101)2 = -(0.1101) x 2 +4

10000100 11010000 00000000 00000000

(c) -0.125= (-0.001)2 = -(0.1) x 2 -2


2’s complement of +2 (+2=0000010 = 1111101+1=1111110)

11111110 10000000 00000000 00000000


3-20. Represent the number (+46.5), as a floating-point binary number with 24 bits. The
normalized fraction mantissa has 16 bits and the exponent has 8 bits.

+(46.5)10 =+ (101110.1)2 = +.1011101 x 2+6


Mantissa (16 bits)= 0 101110100000000
Exponent(8 bits)= 0 0000110

-(46.5)10 =- (101110.1)2 = -.1011101 x 2+6


Mantissa (16 bits)= 1 101110100000000
Exponent(8 bits)= 0 0000110

Advantage of normalization: increases precision

Let the binary number be +000011.101101 =+.000011101101 x 2+6


Store the number in un-normalized and normalized form using mantissa stored in 8 bits
and exponent stored in 4 bits.

Un-normalized form:
M=0 0000111 E=0110 => 0.0000111 x 2+6 = 0.0000111 x 1000000=>000011.1
= (3.5)10

Normalized form: +11.101101 = +0.11101101 x 2+2


M=0 1110110 E=0010 => +0.1110110 x 2+2 =>11.10110 = (3.6875)10

Largest +ve 4 digit decimal number in un-normalized and normalized


form 9999
Smallest +ve 4 digit decimal number in un-normalized form 0001
Smallest +ve 4 digit decimal number in normalized form 1000

Find the largest positive and smallest positive floating point number in un-normalized and
normalized form that can be stored in 4 bit mantissa and 4 bit exponent excluding zero.
Numbers in the mantissa and exponent are in signed-magnitude representation.

Largest positive value in normalized and un-normalized form:


Mantissa= 0,111 = +(0.111)2
Exponent=0,111 =+(7)10 27 =10000000
Expressing in +m x r = +0.111 x 27
+e

Smallest positive value in un-normalized form:


Mantissa= 0,001 = +(0.001)2
Exponent=1,111 = -(7)10
Expressing in +m x r+e = +0.001 x 2-7

Smallest positive value in normalized form:


Mantissa= 0,100 = +(0.100)2
Exponent=1,111 = -(7)10
Expressing in +m x r+e = +0.100 x 2-7

3-19. A 36-bit floating-point binary number has eight bits plus sign for the exponent and 26
bits plus sign for the mantissa. The mantissa is a normalized fraction. Numbers in the
mantissa and exponent are in signed-magnitude representation. What are the largest and
smallest positive quantities that can be represented, excluding zero?

You might also like