Binary Operations 1 2 Decimal Addition Example Add

Binary Operations 1

2 Decimal Addition Example Add 3758 to 4657: 111 3758 + 4657 8 415 1) Add 8 + 7 = 15 Write down 5, carry 1 2) Add 5 + 1 = 11 Write down 1, carry 1 3) Add 7 + 6 + 1 = 14 Write down 4, carry 1 4) Add 3 + 4 + 1 = 8 Write down 8

3 Decimal Addition Explanation 111 3758 + 4657 8415 What just happened? 1 1 1 (carry) 3 7 5 8 +4 6 5 7 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.

4 Binary Addition Rules Truth Table: n 0+0→ 0 n 0+1→ 1 n 1+0→ 1 n 1 + 1 → 0, carry 1 (since 1 + 1 = 0 + 1 × binary 10)

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

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

7 Binary Addition Verification You can always check your answer by converting the figures to decimal, doing the addition, and comparing the answers. + 1 1 0 1 1 1 0 0 1 1 Verification 1101112 +0111002 5510 + 2810 8310 64 32 16 8 4 2 1 1 0 0 1 1 = 64 + 16 + 2 +1 = 8310

8 Binary Addition Example 2 Verification 1110102 Example 2: Add 1111 to 111010. +0011112 1 1 1 + 1 0 1 1 0 0 1 5810 + 1510 7310 64 32 16 8 4 2 1 1 0 0 1 = 64 + 8 +1 = 7310

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

10 Decimal Subtraction Explanation 8 - 4 0 6 2 5 3 3 6 5 7 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.

11 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.

12 Binary Subtraction Explanation Subtraction works in much the same way: Truth Table 0− 0→ 0 0 − 1 → 1, borrow 1 1− 0→ 1 1− 1→ 0

13 Binary Subtraction Example 1: Subtract binary 11100 from 110011 2 0 0 1 1 2 2 0 0 1 1 1 Col 1) Subtract 1 – 0 = 1 Col 2) Subtract 1 – 0 = 1 Col 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. Add the borrowed 2 to the 0 on the right. Now you can borrow from this column (leaving 1 remaining). Add the borrowed 2 to the original 0. Then subtract 2 – 1 = 1 Col 4) Subtract 1 – 1 = 0 Col 5) Try to subtract 0 – 1 can’t. 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.

14 Binary Subtraction Verification 1100112 5110 Subtract binary 11100 from 110011: 2 0 0 1 1 2 2 0 0 1 1 1 - 111002 - 2810 2310 64 32 16 8 4 2 1 1 0 1 1 1 = 16 + 4 + 2 + 1 = 2310

15 Binary Subtraction Example 2 Verification 1010012 4110 Example 2: Subtract binary 10100 from 101001 - 0 2 1 0 1 0 1 101002 - 2010 2110 64 32 16 8 4 2 1 1 0 1 = 16 + 4 + 1 = 2110

Logical, Shift, and Rotate Operations 16

17 Logical, Shift and Rotate Operations § A particular bit, or set of bits, within the byte can be set to 1 or 0, depending on conditions encountered during the execution of a program. § When so used, these bits are often called "flags". § Frequently, the programmer must manipulate these individual bits - an activity sometimes known as "bit twiddling". § The logical, shift, and rotate operations provide the means for manipulating the bits.

18 Logical OR Rules OR Operations OR Results in 1 if either or both of the operands are 1. OR Table 0 OR 0 = 0 0 OR 1 = 1 1 OR 0 = 1 1 OR 1 = 1

19 Logical OR Operation To perform the OR operation, take one column at a time and perform the OR operation using the OR table. Ex 1: 10010011 OR 00001111 10011111

20 Logical OR Examples Ex 2: 11001001 OR 0 0 1 0 11001011 Ex 3: 0111 OR 0010 0111

21 Logical XOR Rules XOR Operations The exclusive OR. Similar to OR except that it now gives 0 when both its operands are 1. Rules. 0 0 1 1 XOR XOR 0=0 1=1 0=1 1=0

22 Logical XOR Examples Ex 1: Ex 2: 1001 XOR 0 0 1 1 10010110 0111 XOR 0 0 1 0 0101

23 Logical AND Rules AND Operations AND yields 1 only if both its operands are 1. Rules. 0 AND 0 = 0 0 AND 1 = 0 1 AND 0 = 0 1 AND 1 = 1

24 Logical AND Examples Ex 1: Ex 2: 11010011 AND 0 0 1 1 00000011 0111 AND 1 0 0 1 0001

25 Logical NOT Operations NOT is a separate operator flipping the bits. Rules. NOT 0 = 1 NOT 1 = 0 Example. NOT 1 0 = 0 1

26 Shift and Rotate operations Whereas logical operations allow the changing of bit values in place, SHIFT and ROTATE operations allow bits to be moved left or right without changing their values.

27 Shift Left operation SHL (shift left) shifts each bit one place to the left. The original leftmost bit is lost and a 0 is shifted into the rightmost position. Ex 1. SHL 1101 1 1 10 01 10 Ex 2. SHL 1100 =1010

28 Shift Left - Multiple Bits SHL Ex 1: # bits means to shift left # times SHL 3 10011100 1 01 01 10 10 10 00 00 = 1 1 1 0 0 0 Ex 2: SHL 2 11100110 =10011000

29 Shift Right operation SHR (shift right) shifts each bit one place to the right. The original rightmost bit is lost and a 0 is shifted into the leftmost position. Ex 1. Ex 2. SHR 1 0 1 1 0101 1 SHR 0 0 1 1 =0001 =0101

30 Shift Right – Multiple Bits SHR # bits means to shift right # times Ex 1: SHR 3 Ex 2: SHR 2 10011100 00010011 100 =00010011 11100110 =00111001

31 Arithmetic Shift Right operation ASR (retains rightmost sign bit) Shifts each bit one place to the right. The original rightmost bit is lost and a the value of the most significant bit (leftmost bit) is shifted into the new leftmost position. Ex 1. ASR 1 0 1 1 1101 1 Ex 2. ASR 0 0 1 1 =0001 =1101

32 Arithmetic Shift Right – Multiple Bits ASR # bits means to arithmetic shift right # times Ex 1: ASR 3 Ex 2: ASR 2 10011100 11110011 100 =11110011 0110 =00011001

33 Rotate Left operation ROL (rotate left) shifts each bit one place to the left. The original leftmost bit is shifted into the rightmost position. No bits are lost. Ex 1. ROL 1 1 0 1 1011 Ex 2. ROL 1100 =1001

34 Rotate Left – Multiple Bits ROL # bits means to rotate left # times Ex 1: ROL 3 10011100 =11100100 Ex 2: ROL 2 11100110 =10011011

35 Rotate Right operation ROR (rotate right) shifts each bit one place to the right. The original rightmost bit is shifted into the leftmost position. No bits are lost. Ex 1. ROR 1 0 1 1 11 0 1 Ex 2. ROR 0 0 1 1 =1001

36 Rotate Right – Multiple Bits ROR # bits means to rotate right # times Ex 1: ROR 3 Ex 2: 10011100 =10010011 ROR 2 1 1 1 0 0 1 1 0 =10111001
- Slides: 36