0% found this document useful (0 votes)
3 views

Lecture 5

The document is a lecture on binary arithmetic, covering addition, subtraction, multiplication, and division in binary systems, along with signed integer representations such as signed magnitude, one's complement, and two's complement. It explains the rules for performing binary operations and the implications of using different signed number systems. Additionally, it discusses overflow rules in binary arithmetic operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture 5

The document is a lecture on binary arithmetic, covering addition, subtraction, multiplication, and division in binary systems, along with signed integer representations such as signed magnitude, one's complement, and two's complement. It explains the rules for performing binary operations and the implications of using different signed number systems. Additionally, it discusses overflow rules in binary arithmetic operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Applications of Information and

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

Four Basic rules for binary addition

1st digit 2nd Sum Carry


digit
0 0 0 0
0 1 1 0
1 0 1 0
1 1 0 1
 Addition of multiple binary numbers
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

Four Basic rules for binary subtraction

1st digit 2nd Differenc Borro


digit e w
0 0 0 0
0 1 1 1
1 0 1 0
1 1 0 0
Binary Subtraction

Borrow 1
1st 1 0 1 1
Number
2nd 1 1 0
Number
Result 1 0 1
Binary Multiplication

Four Basic rules for binary multiplication

1st digit 2nd Product


digit
0 0 0
0 1 0
1 0 0
1 1 1
 Example of Binary Multiplication
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

 Binary 111012 (29) shifted left by one bit


 1110102 (58)
 Shift left 1 bit is multiply by 2
Binary Division

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

 Binary 111012 (29) shifted left by one bit


 1110.12 (14.5)
 Shift left 1 bit is divide by 2
Signed Integers
So far, we have treated all of our numbers as
unsigned (or positive only)
 To implement signed integers (or signed fractions), we need a
mechanism to denote the sign itself (positive or negative)
 Unfortunately, this introduces new problems, so we will see 3
different approaches, all of which add a special bit known as the
sign bit
 If the sign bit is 0, the number is positive
 If the sign bit is 1, the number is negative
 If we have an 8-bit number, does this mean that we now need 9
bits to store it with one bit used exclusively for the sign?
Goal of negative number systems
Signed system: Simple. Just flip the sign bit
0 = positive
1 = negative
One’s complement: Replace subtraction with
addition
Easy to derive (Just flip every bit)
Two’s complement: Replace subtraction with
addition
Addition of one’s complement and one produces the two’s complement.

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

Case II: Numbers: +ve, –ve (-ve is <


+ve )
Add: 5 + (-2) (equivalent to 5 – 2
(subtraction))

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

Case III: Numbers: +ve, –ve (-ve is > +ve


)
Add: (-5) + 2 (equivalent to 2 - 5
(subtraction))

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

Case IV: Numbers: -ve, –ve


Add: (-5) + (-2)

Take 1’s complement to get negative of 0101 (5) =>


1010
Take 1’s complement to get negative of 0010 (2) =>
Carry, add to the result
1101in 1’s
and result
1010 -5 complement form
1101 -2 (again take 1’s)

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))

 Take 1’s complement to get negative of 0010 (2) =>


1101
 Add 1 to 1101 = Carry,
> 1110 so result is +ve
0101 +5 Discard carry

1110 -2
10011
0011 +3
Addition and Subtraction with 2’s
Complement

Case III: Numbers: +ve, –ve (-ve is >


+ve )
Add: (-5) + 2 (equivalent to 2 - 5
(subtraction))

 Take 1’s complement toso


No Carry, get negative
result of 0101
is –ve, and in (5)
=> 1010form (again take
2’s complement
 Add 1 to 1010 => 1011 2’s to verify)
1011 -5
0010 +2
1101 -3
0010 1’s
Addition and Subtraction with 2’s
Complement
Case IV: Numbers: -ve, –ve
Add: (-5) + (-2)

 Take 1’s complement to get negative of 0101 (5) => 1010


 Add 1 to 1010 for 2’s complement => 1011
 Take 1’s complement to get negative of 0010 (2) => 1101
 Add 1 to 1101 for 2’s complement => 1110
1011 -5
Carry, discard it, result is –ve and
1110 -2 result in 2’s complement form
11001 -7 (again take 2’s to verify)
0110 1’s
1 2’s
Note: Avoid overflow
0111 (7)
Some Examples
 Represent 83 and –83 using 8 bits in all 3 signed
representations:
 +83 = 01010011 (in all 3 representations)
 -83:
 Signed magnitude = 11010011 (sign bit is 1 for
negative)
 One’s complement = 10101100 (flip all bits from +83)
 Two’s complement = 10101101 (flip all bits from +83
and add 1)

 Convert 11110010 into a decimal integer in all 4


representations
 Unsigned magnitude = 128 + 64 + 32 + 16 + 2 = 242
 Signed magnitude = -114 (negative, 1110010 = 114)
 One’s complement = -13 (leading bit = 1, the number is
negative, flip all bits 00001101 = 13)
 Two’s complement = -14 (negative, so flip all bits and
Overflow Rules
In unsigned magnitude addition
 a carry out of the left-most bit is also an
overflow
In unsigned magnitude subtraction
 overflow will occur in subtraction if we must
borrow prior to the left-most bit
In two’s complement addition/subtraction
 if the two numbers have the same sign bit and
the sum/difference has a different sign bit, then
overflow
Below we see examples of four signed additions

You might also like