[go: up one dir, main page]

0% found this document useful (0 votes)
154 views29 pages

Binary Math, Signed Unsigned Operations

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 29

Binary Math, Signed Unsigned

Operations
Bijan Paul
Senior Lecturer
Department of CSE
University of Liberal Arts Bangladesh
Decimal Addition Example
1) Add 8 + 7 = 15
Add 3758 to 4657: Write down 5, carry 1

2) Add 5 + 5 + 1 = 11
111 Write down 1, carry 1
3758 3) Add 7 + 6 + 1 = 14
+ 4657 Write down 4, carry 1

8 415 4) Add 3 + 4 + 1 = 8
Write down 8
Decimal Addition Explanation

What just happened?


111
3758 1 1 1 (carry)
3 7 5 8
+ 4657 +4 6 5 7
8415 -
8 14 11 15 (sum)
10 10 10 (subtract the base)

8 4 1 5

So when the sum of a column is equal to or greater than


the base, we subtract the base from the sum, record the
difference, and carry one to the next column to the left.
Binary Addition Rules

Rules:
0+0 =0
0+1 =1
1+0 =1 (just like in decimal)

 1+1 = 210 = 102 = 0


with 1 to carry
 1 + 1 + 1 = 310 = 112 =
1 with 1 to carry
Binary Addition Example 1

Col 1) Add 1 + 0 = 1
Example 1: Add Write 1
binary 110111 to 11100 Col 2) Add 1 + 0 = 1
Write 1
Col 3) Add 1 + 1 = 2 (10 in binary)
1 1 1 1 Write 0, carry 1
1 1 0 1 1 1 Col 4) Add 1+ 0 + 1 = 2
Write 0, carry 1
+ 0 1 1 1 0 0 Col 5) Add 1 + 1 + 1 = 3 (11 in binary)
Write 1, carry 1
1 0 1 0 0 1 1
Col 6) Add 1 + 1 + 0 = 2
Write 0, carry 1
Col 7) Bring down the carried 1
Write 1
Binary Addition Explanation

In the first two columns,


What is actually there were no carries.
happened when we In column 3, we add 1 + 1 = 2
carried in binary? Since 2 is equal to the base, subtract
the base from the sum and carry 1.
1 1 1 1 In column 4, we also subtract
the base from the sum and carry 1.
1 1 0 1 1 1
In column 5, we also subtract
+ 0 1 1 1 0 0 the base from the sum and carry 1.
2 3 2 2 In column 6, we also subtract
- 2 2 2 2 . the base from the sum and carry 1.
In column 7, we just bring down the
1 0 1 0 0 1 1 carried 1
Binary Addition Verification

You can always check your Verification


answer by converting the
figures to decimal, doing the 1101112  5510
addition, and comparing the +0111002 + 2810
answers.
8310

1 1 0 1 1 1 64 32 16 8 4 2 1
+ 0 1 1 1 0 0 1 0 1 0 0 1 1
1 0 1 0 0 1 1 = 64 + 16 + 2 +1
= 8310
Binary Addition Example 2

Example 2: Verification
Add 1111 to 111010. 1110102  5810
+0011112 + 1510
1 1 1 1 1 7310
1 1 1 0 1 0
+ 0 0 1 1 1 1 64 32 16 8 4 2 1
1 0 0 1 0 0 1
1 0 0 1 0 0 1
= 64 + 8 +1
= 7310
Decimal Subtraction Explanation

Subtract 1) Try to subtract 5 – 7  can’t.


Must borrow 10 from next column.
4657 from 8025: Add the borrowed 10 to the original 5.
Then subtract 15 – 7 = 8.
2) Try to subtract 1 – 5  can’t.
7 9 11 Must borrow 10 from next column.
But next column is 0, so must go to
8 10 2 15 column after next to borrow.
- 4 6 5 7 Add the borrowed 10 to the original 0.
Now you can borrow 10 from this column.
3 3 6 8
Add the borrowed 10 to the original 1..
Then subract 11 – 5 = 6
3) Subtract 9 – 6 = 3
4) Subtract 7 – 4 = 3
Decimal Subtraction Explanation

8 0 2 5
- 4 6 5 7
3 3 6 8
 So when you cannot subtract, you borrow from the
column to the left.
 The amount borrowed is 1 base unit, which in
decimal is 10.
 The 10 is added to the original column value, so
you will be able to subtract.
Binary Subtraction
Explanation

 In binary, the base unit is 2

 So when you cannot subtract, you borrow from the


column to the left.
 The amount borrowed is 2.
 The 2 is added to the original column value, so
you will be able to subtract.
Binary Subtraction
Example 1
Col 1) Subtract 1 – 0 = 1
Example 1: Subtract Col 2) Subtract 1 – 0 = 1
binary 11100 from 110011Col 3) Try to subtract 0 – 1  can’t.
Must borrow 2 from next column.
But next column is 0, so must go to
column after next to borrow.
2 1 Add the borrowed 2 to the 0 on the right.
0 0 2 2 Now you can borrow from this column
(leaving 1 remaining).
1 1 0 0 1 1 Add the borrowed 2 to the original 0.
Then subtract 2 – 1 = 1
- 1 1 1 0 0 Col 4) Subtract 1 – 1 = 0
Col 5) Try to subtract 0 – 1  can’t.
1 0 1 1 1 Must borrow from next column.
Add the borrowed 2 to the remaining 0.
Then subtract 2 – 1 = 1
Col 6) Remaining leading 0 can be ignored.
Binary Subtraction
Verification
Subtract binary
Verification
11100 from 110011: 1100112  5110

2 1 - 111002 - 2810
0 0 2 2 2310
1 1 0 0 1 1
- 1 1 1 0 0 64 32 16 8 4 2 1
1 0 1 1 1
1 0 1 1 1
= 16 + 4 + 2 + 1
= 2310
Binary Subtraction
Example 2
Verification
Example 2: Subtract
binary 10100 from 101001 1010012  4110
- 101002 - 2010
2110
0 2 0 2
1 0 1 0 0 1 64 32 16 8 4 2 1
- 1 0 1 0 0 1 0 1 0 1
1 0 1 0 1 = 16 + 4 + 1
= 2110
Binary Complement
• 1's complement:
– A 1's complement system is a system in which
negative numbers are represented by the inverse of the
binary representations of their corresponding positive
numbers.
– One’s complement of an integer is obtained by
complementing each bit; that is replace each bit 0 by a
1 and each 1 by a 0.
• 2's complement:
– Two's complement is the most common method of
representing signed integers on computers. In general,
negation of a number (or reversing the sign) is
performed by taking the two's complement of its binary
form.
– To get the two’s complement of an integer, just add 1 to
it’s one’s complement.
1's and 2’s complement Example
• What is the 1’s complement of 101012 ?
Solution: Changing 1’s to 0’s and 0’s to 1’s, the 1’s
complement is 010102

• Find 2’s complement of 110012 ?


Solution: Changing 1’s to 0’s and 0’s to 1’s, the 1’s
complement is 001102.
Now 001102 + 12 = 001112 is the 2’s complement
Subtraction by 1’s Complement
1. Add extra zero to make the length of both numbers
the same
2. Take 1’s complement of the number to be subtracted
3. Now add the complement with the other number
4. If the sum has an overflow the result is positive.
To get the result, add this overflow at the right
most positive of the sum.
5. If there is no overflow, the result will be negative.
To get the result, take 1’s complement of the sum
again, and attach a negative sign.
Example: 1’s Complement
Evaluate : (i) 110101 – 100101
Solution:1’s complement of 100101 is 011010.
Hence                                         1 1 0 1 0 1
1’s complement of 10011 -              + 0 1 1 0 1 0_
                                Carry over -   1 0 0 1 1 1 1
                                                                        + 1_
                                                             0 1 0 0 0 0
Sum has an overflow the result is positive. Result is 10000

(ii) 101011 – 111001


Solution: 1’s complement of 111001 is 000110.
Hence                                            1 0 1 0 1 1
         1’s complement -      _+ 0 0 0 1 1 0_
                                                 1 1 0 0 0 1
There is no overflow, the result will be 1’s complement of the sum which
is 001110 and attach a negative sign. So result is – 1110
Fractional Example: 1’s Complement
Evaluate : (i) 1011.001 – 110.10
Solution:1’s complement of 0110.100 is 1001.011.
Hence                                          1 0 1 1 . 0 0 1
1’s complement of 10011 -              + 1 0 0 1 . 0 1 1 ____
                                Carry over -    1    0 1 0 0 . 1 0 0
                                                                         + 1_
                                                              0 1 0 0 . 1 0 1
Sum has an overflow the result is positive. Result is 100.101

(ii) 10110.01 – 11010.10


Solution: 1’s complement of 11010.10 is 00101.01
Hence                                             1 0 1 1 0 . 0 1
         1’s complement -      _+ 0 0 1 0 1 . 0 1 _
                                                  1 1 0 1 1 . 1 0
There is no overflow, the result will be 1’s complement of the sum which
is 00100.01 and attach a negative sign. So result is – 100.01
Subtraction by 2’s Complement
1. Add extra zero to make the length of both numbers
the same
2. Take 2’s complement of the number to be subtracted
3. Now add the complement with the other number
4. If the sum has an overflow the result is positive.
To get the result, discard this overflow.
5. If there is no overflow, the result will be negative.
To get the result, take 2’s complement of the sum
again, and attach a negative sign.
Example: 2’s Complement
Evaluate : (i) 110110 - 10110
Solution: Add `0’ in the sixth place to make the length of both number same.
2’s complement of 010110 is (101001 + 1) i.e.101010.
Hence                                         1 1 0 1 1 0
2’s complement of 010110 -             + 1 0 1 0 1 0_
                                Carry over -   1    1 0 0 0 0 0
Sum has an overflow the result is positive. To get the result, discard this
overflow. Result is 10000

(ii)  10110 – 11010


Solution: 2’s complement of 11010 is (00101 + 1) i.e. 00110.
Hence                                            1 0 1 1 0
2’s complement of 010110 -      + 0 0 1 1 0 _
                                                 1 1 1 0 0
As there is no carry over, the result is negative and is obtained by writing
the 2’s complement of 11100 i.e.(00011 + 1) or 00100. Result is – 100.
Fractional Example: 2’s Complement
Evaluate : (i) 1010.11 – 1001.01
Solution: 2’s complement of 1001.01 is (0110.10 + 1) i.e. 0110.11
Hence                                          1 0 1 0 . 1 1
2’s complement of 1001.01 -              + 0 1 1 0 . 1 1 ____
                                Carry over -    1    0 0 0 1 . 1 0
After dropping the carry over we get the result of subtraction as 1.10.
(ii) 10100.01 – 11011.10
Solution: 2’s complement of 11011.10 is (00100.01 + 1) i.e. 00100.10
Hence                                             10100.01
2’s complement of 11011.10 -      _+ 0 0 1 0 0 . 1 0 _
                                                   1 1 0 0 0 . 1 1
As there is no carry over the result is negative and is obtained by writing
the 2’s complement of 11000.11 i.e.(00111.00 + 1) or 111.01. So, result is
– 111.01.
Unsigned Integer
• An unsigned integer is an integer that represents a
magnitude, so it is never negative.
• Because unsigned integers are by definition nonnegative,
none of the bits are needed to represent the sign, and so all
8 bits in a byte, or 16 bits in a word, are available to
represent number.
• The largest unsigned integer that can be stored in a byte is
11111111 = FFh = 255.
• The biggest unsigned integer a 16-bit word can hold is
1111111111111111 = FFFFh = 65535.
• The most significant bit (msb) is the leftmost bit. The least
significant bit (lsb) is the rightmost bit. The lsb of an
integer is 1 if number is odd, and it’s even if the lsb is 0.
Signed Integer
• An signed integer can be positive or negative.
• The msb is reserved for the sign. 1 means negative and 0
means positive.
• Negative integers are stored in the computer in a special
way known as two’s complement.
Example
Question: Find the two’s complement of the two’s complement of 5 for 16 bit.
Solution: 5 = 0000 0000 0000 0101
1’s complement of 5 = 1111 1111 1111 1010
+1
______________________________________
2’s complement of 5 = 1111 1111 1111 1011

1’s complement of 1111 1111 1111 1011 = 0000 0000 0000 0100
+1
________________________________________________________
2’s complement of 2’s complement of 5 = 0000 0000 0000 0101

That’s mean, after complementing 5 two times, the result should be 5.


Example
Question: Show how the decimal integer -97 would be represented (a) in 8
bits, and (b) 16 bits. Express the answers in hexadecimal.
Solution:
(a) In 8 bits, we get 97 = 0110 0001
1’s complement of 97 = 1001 1110
+1
___________________________________
2’s complement of 97 = 1001 1111 = 9Fh

(b) In 8 bits, we get 97 = 0000 0000 0110 0001


1’s complement of 97 = 1111 1111 1001 1110
+1
_______________________________________________
2’s complement of 97 = 1111 1111 1001 1111 = FF9Fh
Subtraction by 2’s Complement
1. Add extra zero to make the length of both numbers
the same
2. Take 2’s complement of the number to be subtracted
3. Now add the complement with the other number
4. If the sum has an overflow the result is positive.
To get the result, discard this overflow.
5. If there is no overflow, the result will be negative.
To get the result, take 2’s complement of the sum
again, and attach a negative sign.
Example: Subtract as 2’s Complement Addition
Question: Suppose AX contains 5ABCh and BX contains 21FCh. Find the
difference of AX minus BX by using complementation and addition.
Solution: AX contains 5ABCh = 0101 1010 1011 1100
BX contains 21FCh = 0010 0001 1111 1100

1’s complement of 21FCh = 1101 1110 0000 0011


+1
__________________________________________________
2’s complement of 21FCh = 1101 1110 0000 0100

Hence, AX contains 5ABCh = 0101 1010 1011 1100


2’s complement of 21FCh = + 1101 1110 0000 0100
__________________________________________________
Difference =10011 1000 1100 0000
Sum has an overflow the result is positive. To get the result, discard this
overflow. Result is 0011 1000 1100 0000 = 38C0h
Thank You!

You might also like