Top Banner
Binary Arithmetic 1. Binary Addition 2. Binary Subtraction a. Direct Method b. r’s complement method c. (r-1)’s complement method 3. Binary Multiplication 4. Binary Division [email protected]
29

Binary Arithmetic - · PDF fileBinary Arithmetic 1. Binary Addition 2. ... = 0.0100. The 1’s complement of a binary number can be obtained ... We consider the subtraction of two

Mar 29, 2018

Download

Documents

phungtuyen
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Binary Arithmetic - · PDF fileBinary Arithmetic 1. Binary Addition 2. ... = 0.0100. The 1’s complement of a binary number can be obtained ... We consider the subtraction of two

Binary Arithmetic

1. Binary Addition

2. Binary Subtraction a. Direct Method

b. r’s complement method

c. (r-1)’s complement method

3. Binary Multiplication

4. Binary Division

[email protected]

Page 2: Binary Arithmetic - · PDF fileBinary Arithmetic 1. Binary Addition 2. ... = 0.0100. The 1’s complement of a binary number can be obtained ... We consider the subtraction of two

Binary Arithmetic -> Addition A B Sum Carry Result 0 0 0 0 0 0 1 1 0 1 1 0 1 0 1 1 1 0 1 10

[email protected]

Add the binary numbers: (a) 1010 and 1101 (b) 0110 and 1111

Page 3: Binary Arithmetic - · PDF fileBinary Arithmetic 1. Binary Addition 2. ... = 0.0100. The 1’s complement of a binary number can be obtained ... We consider the subtraction of two

Binary Arithmetic -> Subtraction A B DIFFERENCE BORROW 0 0 0 0 0 1 1 1 1 0 1 0 1 1 0 0

* * * * (starred columns are borrowed from) 1 1 0 1 1 1 0 1 1 0 − 1 0 1 1 1 - 2 3 ---------------- ------------- 1 0 1 0 1 1 1 8 7

1 0 1 0 0 2 0 − 1 1 1 1 0 - 3 0 ---------------- -------------- 1 1 0 1 1 0 - 1 0

[email protected]

(110110)2=(66)10

Not -10

Page 4: Binary Arithmetic - · PDF fileBinary Arithmetic 1. Binary Addition 2. ... = 0.0100. The 1’s complement of a binary number can be obtained ... We consider the subtraction of two

Complements Complements are used in digital computers for simplifying the subtraction operation and for logical manipulations. there are two type of complements for each number system of base-r: 1. r’s complement 2. (r – 1)’s complement For Decimal r =10 so we can use 10’s complement and 9’s complement For Binary r =2 so we can use 2’s complement and 1’s complement For Decimal r =8 so we can use 8’s complement and 7’s complement

[email protected]

Page 5: Binary Arithmetic - · PDF fileBinary Arithmetic 1. Binary Addition 2. ... = 0.0100. The 1’s complement of a binary number can be obtained ... We consider the subtraction of two

Complements -> r’s Complement

If a positive number N is given in base r with an integer part of n digits

r’s complement = 𝒓𝒏 − 𝑵 𝑵 ≠ 𝟎 𝟎 𝑵 = 𝟎

Let r = 10 1: The 10’s complement of (23450)10 is 105– 23450 The number of digits in the number is n = 5. 2: The 10’s complement of (0.3245)10 is 100 – 0.3245 = 0.6755. Since the number of digits in the integer part of the number is n = 0, we have 100 = 1. 3: The 10’s complement of (23.324)10 is 102 – 23.324 = 76.676. here n=2

1 0 0 0 0 0 - 2 3 4 5 0 --------------- 7 6 5 5 0 10’s complement ---------------

[email protected]

Page 6: Binary Arithmetic - · PDF fileBinary Arithmetic 1. Binary Addition 2. ... = 0.0100. The 1’s complement of a binary number can be obtained ... We consider the subtraction of two

Complements -> r’s Complement

If a positive number N is given in base r with an integer part of n digits

r’s complement = 𝒓𝒏 − 𝑵 𝑵 ≠ 𝟎 𝟎 𝑵 = 𝟎

consider a binary system, then r = 2. 4: The 2’s complement of (10110)2 is (25)10–(10110)2= (100000)2 – (10110)2

5: The 2’s complement of (0.1011)2 is (20)10–(0.1011)2= (1 – 0.1011)2= 0.0101

2’s complement of a binary number can be obtained by leaving all of the least significant zeros and the first nonzero digit unchanged, and then replacing 1’s with 0’s and 0’s with 1’s

1 0 0 0 0 0 - 1 0 1 1 0 ------------------ 0 1 0 1 0 2’s complement ------------------

[email protected]

Page 7: Binary Arithmetic - · PDF fileBinary Arithmetic 1. Binary Addition 2. ... = 0.0100. The 1’s complement of a binary number can be obtained ... We consider the subtraction of two

Complements -> (r-1)’s Complement

If a positive number N is given in base r with an integer part of n digits and a fraction part of m digits

(r-1)’s complement = 𝒓𝒏 − 𝒓 _ 𝒎 − 𝑵 𝑵 ≠ 𝟎

𝟎 𝑵 = 𝟎

consider a binary system, then r = 2. 3: The 1’s complement of (10110)2 is (25–1)10– (10110)2 = (100000 - 1 - 10110 )2

4: The 1’s complement of (0.1011)2 is (1–2–4)10– (0.1011)2 = 0.0100.

The 1’s complement of a binary number can be obtained by changing 1s into 0s and 0s into 1s

1 0 0 0 0 0 - 1 0 1 1 0 ------------------ 0 1 0 1 0 - 1 ------------------ 0 1 0 0 1 2’s complement

[email protected]

Page 8: Binary Arithmetic - · PDF fileBinary Arithmetic 1. Binary Addition 2. ... = 0.0100. The 1’s complement of a binary number can be obtained ... We consider the subtraction of two

Complements -> (r-1)’s Complement

If a positive number N is given in base r with an integer part of n digits and a fraction part of m digits

(r-1)’s complement = 𝒓𝒏 − 𝒓 _ 𝒎 − 𝑵 𝑵 ≠ 𝟎

𝟎 𝑵 = 𝟎

consider r = 10. 1: The 9’s complement of (23450)10 is 105 – 100 – 23450 = 76549.

2: The 9’s complement of (23.324)10 is 102– 10–3 – 23.324 = 76.675

1 0 0 0 0 0 - 2 3 4 5 0 --------------- 7 6 5 5 0 - 1 --------------- 7 6 5 4 9 9’s complement

[email protected]

Page 9: Binary Arithmetic - · PDF fileBinary Arithmetic 1. Binary Addition 2. ... = 0.0100. The 1’s complement of a binary number can be obtained ... We consider the subtraction of two

Binary Arithmetic -> Subtraction A B DIFFERENCE BORROW 0 0 0 0 0 1 1 1 1 0 1 0 1 1 0 0

* * * * (starred columns are borrowed from) 1 1 0 1 1 1 0 1 1 0 − 1 0 1 1 1 - 2 3 ---------------- ------------- 1 0 1 0 1 1 1 8 7

1 0 1 0 0 2 0 − 1 1 1 1 0 - 3 0 ---------------- --------------- 1 1 0 1 1 0 9 9 0

I am in 10’s complement

form

[email protected]

Page 10: Binary Arithmetic - · PDF fileBinary Arithmetic 1. Binary Addition 2. ... = 0.0100. The 1’s complement of a binary number can be obtained ... We consider the subtraction of two

Binary Arithmetic -> Subtraction-> Direct Method

A B DIFFERENCE BORROW 0 0 0 0 0 1 1 1 1 0 1 0 1 1 0 0

* * * * (starred columns are borrowed from) 1 1 0 1 1 1 0 1 1 0 − 1 0 1 1 1 - 2 3 ---------------- ------------- 1 0 1 0 1 1 1 8 7

1 0 1 0 0 2 0 − 1 1 1 1 0 - 3 0 ---------------- --------------- 1 1 0 1 1 0 9 9 0

[email protected]

When smaller number is subtracted from larger the result of subtraction is positive and in the direct method the result obtained is OK.

Page 11: Binary Arithmetic - · PDF fileBinary Arithmetic 1. Binary Addition 2. ... = 0.0100. The 1’s complement of a binary number can be obtained ... We consider the subtraction of two

Binary Arithmetic -> Subtraction-> Direct Method

A B DIFFERENCE BORROW 0 0 0 0 0 1 1 1 1 0 1 0 1 1 0 0

* * * * (starred columns are borrowed from) 1 1 0 1 1 1 0 1 1 0 − 1 0 1 1 1 - 2 3 ---------------- ------------- 1 0 1 0 1 1 1 8 7

1 0 1 0 0 2 0 − 1 1 1 1 0 - 3 0 ---------------- --------------- 1 1 0 1 1 0 9 9 0

[email protected]

In the direct method, whenever a larger number is subtracted from a smaller number, the result obtained is in 2’s complement form and opposite in sign. To get the true result we have to discard the carry and make the 2’s complement of the result obtained and put a negative sign before the result

Page 12: Binary Arithmetic - · PDF fileBinary Arithmetic 1. Binary Addition 2. ... = 0.0100. The 1’s complement of a binary number can be obtained ... We consider the subtraction of two

Binary Arithmetic -> Subtraction-> 1’s Comp. Method

Binary subtraction can be performed by adding the 1’s complement of the subtrahend to the minuend. If a carry is generated, remove the carry, add it to the result. This carry is called the end-around carry. Now if the subtrahend is larger than the minuend, then no carry is generated. The answer obtained is 1’s complement of the true result and opposite in sign.

[email protected]

Subtract (1001)2 from (1101)2 using the 1’s complement method. Also subtract using the direct method and compare

Page 13: Binary Arithmetic - · PDF fileBinary Arithmetic 1. Binary Addition 2. ... = 0.0100. The 1’s complement of a binary number can be obtained ... We consider the subtraction of two

Binary Arithmetic -> Subtraction-> 1’s Comp. Method

Binary subtraction can be performed by adding the 1’s complement of the subtrahend to the minuend. If a carry is generated, remove the carry, add it to the result. This carry is called the end-around carry. Now if the subtrahend is larger than the minuend, then no carry is generated. The answer obtained is 1’s complement of the true result and opposite in sign.

[email protected]

Subtract (1100)2 from (1001)2 using the 1’s complement method. Also subtract using the direct method and compare

Page 14: Binary Arithmetic - · PDF fileBinary Arithmetic 1. Binary Addition 2. ... = 0.0100. The 1’s complement of a binary number can be obtained ... We consider the subtraction of two

Binary Arithmetic -> Subtraction-> 1’s Comp. Method

Binary subtraction can be performed by adding the 1’s complement of the subtrahend to the minuend. If a carry is generated, remove the carry, add it to the result. This carry is called the end-around carry. Now if the subtrahend is larger than the minuend, then no carry is generated. The answer obtained is 1’s complement of the true result and opposite in sign.

[email protected]

If subtrahend is larger than the minuend, In the 1’s complement subtraction, no carry is obtained and the result obtained is in 1’s complement form. To get the true result we have to make the 1’s complement of

the result obtained and put a negative sign before the result

Page 15: Binary Arithmetic - · PDF fileBinary Arithmetic 1. Binary Addition 2. ... = 0.0100. The 1’s complement of a binary number can be obtained ... We consider the subtraction of two

Binary Arithmetic -> Subtraction-> 2’s Comp. Method

Binary subtraction can be performed by adding the 2’s complement of the subtrahend to the minuend. If a carry is generated, discard the carry. Now if the subtrahend is larger than the minuend, then no carry is generated. The answer obtained is in 2’s complement and is negative. To get a true answer take the 2’s complement of the number and change the sign

[email protected]

Subtract (0111)2 from (1101)2 using the 2’s complement method. Also subtract using the direct method and compare

2

Page 16: Binary Arithmetic - · PDF fileBinary Arithmetic 1. Binary Addition 2. ... = 0.0100. The 1’s complement of a binary number can be obtained ... We consider the subtraction of two

Binary Arithmetic -> Subtraction-> 2’s Comp. Method

Binary subtraction can be performed by adding the 2’s complement of the subtrahend to the minuend. If a carry is generated, discard the carry. Now if the subtrahend is larger than the minuend, then no carry is generated. The answer obtained is in 2’s complement and is negative. To get a true answer take the 2’s complement of the number and change the sign

[email protected]

Subtract (1010)2 from (1001)2 using the 2’s complement method. Also subtract using the direct method and compare

2

Page 17: Binary Arithmetic - · PDF fileBinary Arithmetic 1. Binary Addition 2. ... = 0.0100. The 1’s complement of a binary number can be obtained ... We consider the subtraction of two

Binary Arithmetic -> Subtraction-> 2’s Comp. Method

Binary subtraction can be performed by adding the 2’s complement of the subtrahend to the minuend. If a carry is generated, discard the carry. Now if the subtrahend is larger than the minuend, then no carry is generated. The answer obtained is in 2’s complement and is negative. To get a true answer take the 2’s complement of the number and change the sign

[email protected]

Now if the subtrahend is larger than the minuend, In the 2’s complement subtraction, no carry is obtained and the result obtained is in 2’s complement form. To get the true result we have to make the 2’s complement of the result obtained and put a negative sign before the result

Page 18: Binary Arithmetic - · PDF fileBinary Arithmetic 1. Binary Addition 2. ... = 0.0100. The 1’s complement of a binary number can be obtained ... We consider the subtraction of two

Subtraction-> 1’s Comp Vs. 2’s Comp. Method 1: The 1’s complement has the advantage of being easier to implement by digital components (viz. inverter) since the only thing to be done is to change the 1s to 0s and vice versa. To implement 2’s complement we can follow two ways: (1) by finding out the 1’s complement of the number and then adding 1 to the LSB of the 1’s complement, and (2) by leaving all leading 0s in the LSB positions and the first 1 unchanged, and only then changing all 1’s to 0s and vice versa.

[email protected]

2: During subtraction of two numbers by a complement method, the 2’s complement is advantageous since only one arithmetic addition is required. The 1’s complement requires two arithmetic additions when an end-around carry occurs.

3: The 1’s complement has an additional disadvantage of having two arithmetic zeros: one with all 0s and one with all 1s. The 2’s complement has only one arithmetic zero. The fact is illustrated below: We consider the subtraction of two equal binary numbers 1010 – 1010 1010 + 0101 (1’s complement of 1010) + 1111 (negative zero) We complement again to obtain (– 0000) (positive zero)

Page 19: Binary Arithmetic - · PDF fileBinary Arithmetic 1. Binary Addition 2. ... = 0.0100. The 1’s complement of a binary number can be obtained ... We consider the subtraction of two

Unsigned Binary Numbers If we have 3 bit computer than it can handle 23 = 8 numbers (0-7) numbers. Up till now we have placed – sign to sow negative numbers but in computer we have only 0’s and 1’s

[email protected]

Decimal Unsigned Binary

0 000

1 001

2 010

3 011

4 100

5 101

6 110

7 111

Page 20: Binary Arithmetic - · PDF fileBinary Arithmetic 1. Binary Addition 2. ... = 0.0100. The 1’s complement of a binary number can be obtained ... We consider the subtraction of two

Signed Binary Numbers In digital circuits, there is no provision to put a plus or minus sign, since everything in digital circuits have to be represented in terms of 0 and 1. Normally an additional bit is used as the sign bit. This sign bit is usually placed as the MSB. Generally a 0 is reserved for a positive number and a 1 is reserved for a negative number.

[email protected]

three types of representations of signed binary numbers • sign-magnitude representation • 1’s complement representation • 2’s complement representations

Page 21: Binary Arithmetic - · PDF fileBinary Arithmetic 1. Binary Addition 2. ... = 0.0100. The 1’s complement of a binary number can be obtained ... We consider the subtraction of two

Signed Binary Numbers->sign-magnitude representation

• MSB bit is sign bit, rest bits shows magnitude of number. • If MSB is 0, positive number • If MSB is 1, negative number

[email protected]

Find the decimal equivalent of the following binary numbers assuming the binary numbers have been represented in sign-magnitude form. (a) 0101100 (b) 101000 (c) 1111 (d) 011011 (a) Sign bit is 0, which indicates the number is positive. Magnitude 101100 = (44)10

Therefore (0101100)2= (+44)10

(b) Sign bit is 1, which indicates the number is negative. Magnitude 01000 = (8)10

Therefore (101000)2= (–8)10

0101100= 0 101100 sign mag.

Page 22: Binary Arithmetic - · PDF fileBinary Arithmetic 1. Binary Addition 2. ... = 0.0100. The 1’s complement of a binary number can be obtained ... We consider the subtraction of two

Signed Binary Numbers->sign-magnitude representation

• MSB bit is sign bit, rest bits shows magnitude of number. • If MSB is 0, positive number • If MSB is 1, negative number

[email protected]

0101100= 0 101100 sign mag.

Decimal Sign-Magnitude Representation

+0 0 00

+1 0 01

+2 0 10

+3 0 11

-0 1 00

-1 1 01

-2 1 10

-3 1 11

Page 23: Binary Arithmetic - · PDF fileBinary Arithmetic 1. Binary Addition 2. ... = 0.0100. The 1’s complement of a binary number can be obtained ... We consider the subtraction of two

Signed Binary Numbers->1’s Comp. representation • both numbers are 1’s complement of each other. If one numbers is positive, then the

other will be negative with the same magnitude and vice versa. • If MSB is 0, positive number • If MSB is 1, negative number

[email protected]

Represent the following numbers in 1’s complement form. (a) +5 and –5 (b) +9 and –9 (c) +15 and –15 (a) (+5)10 = (0101)2

(–5)10= (1010)2

(b) (+9)10 = (01001)2

(–9)10 = (10110)2

For an n-bit number, the maximum positive number which can be represented in 1’s complement form is (2n–1–1) and the maximum negative number is –(2n–1– 1)

0101= 0 101 + mag. 1010= 1 010 - mag. is 1’s Comp

Page 24: Binary Arithmetic - · PDF fileBinary Arithmetic 1. Binary Addition 2. ... = 0.0100. The 1’s complement of a binary number can be obtained ... We consider the subtraction of two

Signed Binary Numbers->1’s Comp. representation • both numbers are 1’s complement of each other. If one numbers is positive, then the

other will be negative with the same magnitude and vice versa. • If MSB is 0, positive number • If MSB is 1, negative number

[email protected]

010= 0 10 + mag. 101= 1 01 - mag. is 1’s Comp

Decimal Sign-Mag.

1’s Comp

+0 0 00 0 00

+1 0 01 0 01

+2 0 10 0 10

+3 0 11 0 11

-0 1 00 1 11

-1 1 01 1 10

-2 1 10 1 01

-3 1 11 1 00

Page 25: Binary Arithmetic - · PDF fileBinary Arithmetic 1. Binary Addition 2. ... = 0.0100. The 1’s complement of a binary number can be obtained ... We consider the subtraction of two

Signed Binary Numbers->2’s Comp. representation • both numbers are 2’s complement of each other. If one numbers is positive, then the

other will be negative with the same magnitude and vice versa. • If MSB is 0, positive number • If MSB is 1, negative number

[email protected]

Represent the following numbers in 1’s complement form. (a) +5 and –5 (b) +8 and –8 (c) +15 and –15 (a) (+5)10 = (0101)2

(–5)10= (1011)2

(b) (+8)10 = (01000)2

(–8)10 = (11000)2

For an n-bit number, the maximum positive number which can be represented in 2’s complement form is (2n–1–1) and the maximum negative number is –2n–1

0101= 0 101 + mag. 1011= 1 011 - mag. is 2’s Comp

Page 26: Binary Arithmetic - · PDF fileBinary Arithmetic 1. Binary Addition 2. ... = 0.0100. The 1’s complement of a binary number can be obtained ... We consider the subtraction of two

Signed Binary Numbers->2’s Comp. representation • both numbers are 2’s complement of each other. If one numbers is positive, then the

other will be negative with the same magnitude and vice versa. • If MSB is 0, positive number • If MSB is 1, negative number

[email protected]

0101= 0 101 + mag. 1011= 1 011 - mag. is 2’s Comp

Decimal

Sign-Mag.

1’s Comp

2’s Comp

+0 0 00 0 00 0 00

+1 0 01 0 01 0 01

+2 0 10 0 10 0 10

+3 0 11 0 11 0 11

-0 1 00 1 11 - -

-1 1 01 1 10 1 11

-2 1 10 1 01 1 10

-3 1 11 1 00 1 01

-4 1 00

Page 27: Binary Arithmetic - · PDF fileBinary Arithmetic 1. Binary Addition 2. ... = 0.0100. The 1’s complement of a binary number can be obtained ... We consider the subtraction of two

Signed Binary Numbers In digital circuits, there is no provision to put a plus or minus sign, since everything in digital circuits have to be represented in terms of 0 and 1. Normally an additional bit is used as the sign bit. This sign bit is usually placed as the MSB. Generally a 0 is reserved for a positive number and a 1 is reserved for a negative number.

[email protected]

Represent (–19)10 in (a) Sign-magnitude (b) one’s complement (c) two’s complement representation.

Page 28: Binary Arithmetic - · PDF fileBinary Arithmetic 1. Binary Addition 2. ... = 0.0100. The 1’s complement of a binary number can be obtained ... We consider the subtraction of two

Example A = +9 B = +13 Find A+B, A-B and B-A if computer can do only 1’s compliment and addition, Moreover computer can handle 8 bits only. How many numbers computer can work on?

[email protected]

Solution:

A=

B= A + B= 0001 0110

___________________________________________________________________________

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

A + Bc =1111 1011 Bc =

___________________________________________________________________________ -A + B = Ac + B Ac =

Ac + B= 0000 0100 B =

0 0 0 0 1 0 0 1

0 0 0 0 1 1 0 1

0 0 0 0 1 0 0 1

1 1 1 1 0 0 1 0

0 0 0 0 1 1 0 1

1 1 1 1 0 1 1 0

Page 29: Binary Arithmetic - · PDF fileBinary Arithmetic 1. Binary Addition 2. ... = 0.0100. The 1’s complement of a binary number can be obtained ... We consider the subtraction of two

Home Work A = -9 B = -13 If a computer can handle 8 bits only. How many signed numbers computer can work on? Find A+B, A-B and B-A if computer can do only 1’s compliment and addition. Repeat same problem with 2’s complement.

[email protected]