Lecture 5
Lecture 5
Communication Technology
Lecture # 5
Muhammad Nasir
Department of Computer Science
CUI Lahore Campus
[email protected]
The slides are adapted from the publisher’s material
Computer Science Illuminated (Chapter 2)
&
Internet Sources
Binary Arithmetic
Binary Addition
Binary Subtraction
Binary Multiplication
Binary Division
Binary Addition
Carry 1 10 1
1st 1 0 1 1
Number
2nd 1 1 0
Number
3rd 1 0 0 0
Number
4th 1 1
Number
Binary Subtraction
Borrow 1
1st 1 0 1 1
Number
2nd 1 1 0
Number
Result 1 0 1
Binary Multiplication
1101 (13)
x 101 (5)
1st product term 1101
2nd product term 0000
3rd product term 1101
Product 1000001 (65)
Multiplication by shifting left
Decimal 29 shifted left by one digit
290
Shift left 1 digit is multiply by 10
10
101 | 1101
101
011
000
11
Division by shifting right
Decimal 29 shifted right by one digit
2.9
Shift left 1 digit is divide by 10
13
Signed Magnitude
The first signed integer format is signed magnitude
where we add a bit to the front of our numbers that
represents the sign
In 4 bits, 3 = 0011 and –3 = 1011
Notice in 4 bits, we can store 16 numbers in unsigned
magnitude (0000 to 1111, or decimal 0 to 15) but in signed
magnitude we can only store 15 numbers (between –7, or 1111,
and +7, 0111), so we lose a number
Two problems:
0 is now represented in two ways: 0000, 1000, so we lose the
ability to store an extra number since we have two 0s
We cannot do ordinary arithmetic operations using signed
magnitude
we have to “strip” off the sign bit, perform the operation,
and insert the sign bit on the new answer – this requires
extra hardware
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.
Definitions:
4-Bit Example
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
Signed & 1’s: -(2n-1 – 1) , (2n-1 – 1)
calculating the
range 2’s: -2n-1 , (2n-1 – 1)
One’s Complement
An alternative approach to signed magnitude is
one’s complement where the first bit is again a
sign bit
But negative numbers are stored differently from
positive numbers
Positive number stored as usual
Negative number – all bits are inverted
0s become 1s, 1s become 0s
Example: +19 in 6 bits = 010011, -19 = 101100
The first bit is not only the sign bit, but also part of the
number
Notice that we still have two ways to represent 0,
000000 and 111111
So, we won’t use one’s complement
Addition and Subtraction with 1’s
Complement
Case I: Numbers: +ve, +ve (Normal
Addition)
Add : 5 + 2
0101 +5
0010 +2
0111 +7
Addition and Subtraction with 1’s
Complement
Carry,
Take 1’s complement to so
getresult is +ve of 0010 (2) =>
negative
Add carry
1101
0101 +5
1101 -2
10010
1
0011 +3
Addition and Subtraction with 1’s
Complement
No Carry, so result is –
Take 1’s ve,get
complement to andnegative
in 1’s of 0101 (5) =>
complement
1010 form
(again take 1’s)
1010 -5
0010 +2
1100
0011 -3
Addition and Subtraction with 1’s
Complement
10111
1 Note: Avoid overflow
1000 -7
Two’s Complement
Positive numbers remain the 4-bit Two’s Complement
same Binary Decimal
1000 -8
Negative numbers: derived by 1001 -7
flipping each bit and then 1010
1011
-6
-5
adding 1 to the result 1100 -4
+19 in 6 bits = 010011, 1101 -3
1110 -2
-19 in 6 bits = 101101 1111 -1
010011 101100 +1 101101 0000 0
To convert back, flip all bits and add 0001 1
0010 2
1 0011 3
101101 010010 + 1 010011 0100 4
While this is harder, it has two 0101 5
advantages 0110 6
Only 1 way to represent 0 (000000) so0111 7
we can store 1 extra value that we lost
when we tried signed magnitude and
Addition and Subtraction with 2’s
Complement
Case I: Numbers: +ve, +ve (Normal
Addition)
Add : 5 + 2
0101 +5
0010 +2
0111 +7
Addition and Subtraction with 2’s
Complement
Case II: Numbers: +ve, –ve (-ve is < +ve )
Add: 5 + (-2) (equivalent to 5 – 2
(subtraction))
1110 -2
10011
0011 +3
Addition and Subtraction with 2’s
Complement