You are on page 1of 34

FAST- National University of Computer and

Emerging Sciences, CFD Campus

Computer Logic Design


Sara Saleem, School of Computing
Spring 2014
Lecture 2

Slides Courtesy: Dr. Waseem Ikram


Ask INTELLIGENT
questions!
Recap
Last lecture discussion
• Decimal Number Systems
• Caveman Base 5 Number System
• Binary Number System
• Number System Conversion
Roadmap
• Binary to Decimal Conversion
• Decimal to Binary Conversion
• Binary-Decimal Fraction Conversion
• Binary Arithmetic
• Signed and Unsigned Numbers
• Range of Numbers
Binary to Decimal Conversion-I
• Method 1
• 100112 to Decimal?
• (1 x 24) + (0 x 23) + (0 x 22) + (1 x 21) + (1 x 20)
• Terms 16, 0, 0, 2 and 1
• 19
Essentially Sum-of-Weights
Weight: determined by position and obtained by multiplying
coefficient with respective power of base.
Binary to Decimal Conversion-II
• Method 2
• Add weights of non-zero terms
• Weights increase/decrease by power of 2
• 100112 = 16 + 2 + 1 = 19
• 1011.1012 = 8 + 2 + 1 + 1/2 + 1/8
= 11 + 5/8
= 11.625
Essentially Sum of Non-Zero Terms
Binary to Decimal Conversion-Summary
• Sum-of-Weights
• Expression base number & weights
• Sum terms
• Paper and pencil method
• Sum of non-zero terms
• Mental Arithmetic, quick method
• Sum of weights of non-zero terms
Decimal to Binary Conversion
• Sum-of-Weights method used in reverse
• Highest binary weight less than the decimal number
• Subsequent smaller weights that add up to decimal number
• Repeated division by 2
• Paper and pencil method
• Number repeatedly divided by 2
Decimal to Binary Conversion-I
• Sum-of-Weights number Weight Result after subtraction Binary Bit
• 392 to Binary? 392 256 392-256 = 136 1 b8
• What is the highest 136 128 136-128 = 8 1 b7
power of 2 (weight) less
8 64 0 b6
than 392? (256=28)
• List all the lower 8 32 0 b5
weights (256,128,64,…) 8 16 0 b4
• At each step if 8 8 8-8 =0 1 b3
weight<=number, 0 4 0 b2
subtract weight from
0 2 0 b1
original number and
add 1 for that weight 0 1 0 b0
1100010002
Decimal to Binary Conversion-II
number Quotient after Remainder after division
• Repeated Division
division
by 2
• 392 to Binary? 392 196 0 (b0)
• Original number is 196 98 0 (b1)
divided by 2, 98 49 0 (b2)
quotient goes to 49 24 1 (b3)
next division cycle, 24 12 0 (b4)
remainder is noted 12 6 0 (b5)
• Repeat process until
6 3 0 (b6)
quotient value
becomes 0 3 1 1 (b7)
1 0 1 (b8)
1100010002
Roadmap
• Binary to Decimal Conversion
• Decimal to Binary Conversion
• Binary-Decimal Fraction Conversion
• Binary Arithmetic
• Signed and Unsigned Numbers
• Range of Numbers
Fraction Conversion
• Binary to Decimal Conversion
• Sum-of-Weights method
• Weights decrease by a factor of 2
• 0.11012 weights ½, ¼, 1/16, Sum up to 0.8125
• Decimal to Binary Conversion
• Repeated Multiplication by 2
Fraction Conversion
• Decimal to Binary Conversion
• Repeated multiplication by Number Mult. By 2 Integer
2
0.8125 1.625 1 (b-1)
• Note integer part at each
step 0.625 1.250 1 (b-2)
• Fraction part goes to
0.250 0.500 0 (b-3)
multiplication cycle
• The process is continued 0.500 1.000 1 (b-4)
until the fraction part
becomes zero or number of 0.11012
digits have sufficient
accuracy
Roadmap
• Binary to Decimal Conversion
• Decimal to Binary Conversion
• Binary-Decimal Fraction Conversion
• Binary Arithmetic
• Signed and Unsigned Numbers
• Range of Numbers
Binary Arithmetic
• Binary Addition
• Binary Subtraction
• Binary Multiplication
• Binary Division
Binary Addition
• Four Basic rules for binary addition
• 9 + 3? What is sum? What is carry?

1st digit 2nd digit Sum Carry


0 0 0 0
0 1 1 0
1 0 1 0
1 1 0 1
Addition of multiple binary numbers
• At bit 2 the carry is 10..2-bit
carry? Carry 1 10 1
• Consider adding the number
19, fifteen times. 1st Number 1 0 1 1
• Adding the first digit 9,
fifteen times results in 135. 5 2nd Number 1 1 0
is the Sum and 13 is the carry.
3rd Number 1 0 0 0
• The carry 13 added with
fifteen 1s of the second digit 4th Number 1 1
result in 28.
• The answer is 285. Result 1 1 1 0 0
Binary Subtraction
• Four Basic rules for binary subtraction
• 21-17? What is borrowed? What is difference?

1st digit 2nd digit Difference Borrow


0 0 0 0
0 1 1 1
1 0 1 0
1 1 0 0
Binary Subtraction

Borrow 1
1st Number 1 0 1 1
2nd Number 1 1 0
Result 1 0 1
Binary Multiplication
• Four Basic rules for binary multiplication
• Anything multiplied by 0 results in 0 just like in decimal
multiplication.
1st digit 2nd digit Product
0 0 0
0 1 0
1 0 0
1 1 1
Binary Multiplication
1101 (13)
x 101 (5)
1st product term 1101
2nd product term 0000x
3rd product term 1101xx
Product 1000001 (65)

• Same as decimal multiplication!


Multiplication by shifting left
• Decimal 29 (29.0) shifted left by one digit
• 290
• Shift left 1 digit is multiply by 10 (base)

• Binary 111012 (29) shifted left by one bit


• 1110102 (58)
• Shift left 1 bit is multiply by 2 (base)
Binary Division
1011
11 |100001
11
100
11
11
11
0
• Follows the same rules as decimal division but values will go either 1
times or 0 times
Division by shifting right
• Decimal 29 shifted right by one digit
• 2.9
• Shift left 1 digit is divide by 10

• Binary 111012 (29) shifted left by one bit


• 1110.12 (14.5)
• Shift left 1 bit is divide by 2
Roadmap
• Binary to Decimal Conversion
• Decimal to Binary Conversion
• Binary-Decimal Fraction Conversion
• Binary Arithmetic
• Signed and Unsigned Numbers
• Range of Numbers
Complements
• Given
• Number N having n digits
• Base- r
• Diminished Radix Complement
• (rn -1) -N
• Radix Complement
• rn -N = Diminished Radix Complement + 1
Signed and Unsigned Numbers
• Unsigned Binary Numbers
• Signed Binary Numbers
• Most significant bit represents sign
• 0 represents a positive number
• 1 represents a negative number
2’s Complement form
• 1’s complement form
• 2’s complement form (Negates a number)
+N 2’s -N 2’s +N
Complement Complement

Binary number 01101 (13)


1’s complement 10010
+ 1
2’s complement 10011 (-13)
• Positive numbers-in original form
• Negative numbers in 2’s complement form
Decoding 2’s complement Numbers
• Find sign, find absolute value.
• Example: Binary Numbers : 011, 101
• What is MSB?
• 011, MSB=0  +ve Number
• Binary value of remaining bits give absolute value. 11=3 (In Decimal)
Number is +3
01
• 101, MSB=1  -ve Number
10(1’s comp)
• Take 2’s complement of reaming bits to get absolute value.
+1
11=3. Number is -3 11(2’s comp)
Addition and Subtraction with 2’s
Complement
0101 +5 0101 +5
0010 +2 1110 -2
0111 +7 0011 +3

1011 -5 1011 -5
1110 -2 0010 +2
1001 -7 1101 -3
• Rule: Discard Carry-out from MSB
• What’s the advantage?
• Computer processes them efficiently
• No need to separate signs for addition/subtraction.
• One single system for addition/subtraction
Roadmap
• Binary to Decimal Conversion
• Decimal to Binary Conversion
• Binary-Decimal Fraction Conversion
• Binary Arithmetic
• Signed and Unsigned Numbers
• Range of Numbers
Range of Numbers
• Maximum Range
• Number of digits
• Decimal number example
• Binary number example
• Overflow
Range of Binary Numbers
Decimal Sign Magnitude 2’s Complement Unsigned
Number 2n-1, 2n-1 -1 2n-1, 2n-1 -1 2n, 2n - 1
0 0000 0000 000
1 0001 0001 001
2 0010 0010 010
3 0011 0011 011
4 0100 0100 100
5 0101 0101 101
6 0110 0110 110
7 0111 0111 111
Range of Binary Numbers
Decimal Sign Magnitude 2’s Complement Unsigned
Number 2n-1-1, -(2n-1 -1) 2n-1, -2n-1
-8 1000
-7 1111 1001
-6 1110 1010
-5 1101 1011
-4 1100 1100
-3 1011 1101
-2 1010 1110
-1 1001 1111

You might also like