Download presentation

Presentation is loading. Please wait.

1
Logical, Shift, and Rotate Operations CS208

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

3
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

4
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: 1 0 0 1 0 0 1 1 OR0 0 0 0 1 1 1 1 1 0 0 1 1 1 1 1

5
Logical OR Examples Ex 3: 0 1 1 1 OR0 0 1 0 0 1 1 1 Ex 2: 1 1 0 0 1 0 0 1 OR0 0 0 0 1 0 1 0 1 1 0 0 1 0 1 1

6
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 XOR 0 = 0 0 XOR 1 = 1 1 XOR 0 = 1 1 XOR 1 = 0

7
Logical XOR Examples Ex 1:1 0 0 1 1 0 0 1 XOR0 0 0 0 1 1 1 1 1 0 0 1 0 1 1 0 Ex 2: 0 1 1 1 XOR0 0 1 0 0 1 0 1

8
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

9
Logical AND Examples Ex 1: 1 1 0 1 0 0 1 1 AND 0 0 0 0 1 1 1 1 0 0 0 0 0 0 1 1 Ex 2: 0 1 1 1 AND 1 0 0 1 0 0 0 1

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

11
Logical Operations – Try It Yourself Complete the following exercises: 1 0 0 1 1 0 0 1 OR 0 1 0 1AND 0 1 0 1 1 0 0 1 XOR 0 1 0 1NOT 10010111 = (Answers on next slide)

12
Answers OR 1 1 0 1 AND 0 0 0 1 XOR 1 1 0 0 NOT 0 1 1 0 1 0 0 0

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

14
Shift Left operation SHL 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.SHL1 1 0 1 Ex 2.SHL1 1 0 0 = 1 0 0 0 01 1 0 1 = 1 0 1 0

15
Shift Left - Multiple Bits SHL # bits means to shift left # times Ex 1: SHL 31 0 0 1 1 1 0 0 Ex 2: SHL 21 1 1 0 0 1 1 0 = 1 0 0 1 1 0 0 0 1 0 0 1 1 1 0 0 0 0 0= 1 1 1 0 0 0 0 0

16
Shift Right operation SHR 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.SHR1 0 1 1 Ex 2.SHR0 0 1 1 = 0 0 0 1 0 1 0 1 1 = 0 1 0 1

17
Shift Right – Multiple Bits SHR # bits means to shift right # times Ex 1: SHR 31 0 0 1 1 1 0 0 0 0 0 1 0 0 1 1 1 0 0 = 0 0 0 1 0 0 1 1 Ex 2: SHR 21 1 1 0 0 1 1 0 = 0 0 1 1 1 0 0 1

18
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.ASR1 0 1 1 Ex 2.ASR0 0 1 1 = 0 0 0 1 1 1 0 1 1= 1 1 0 1

19
Arithmetic Shift Right – Multiple Bits ASR # bits means to arithmetic shift right # times Ex 1: ASR 31 0 0 1 1 1 0 0 1 1 1 1 0 0 1 1 1 0 0 = 1 1 1 1 0 0 1 1 Ex 2: ASR 20 1 1 0 0 1 1 0 = 0 0 0 1 1 0 0 1

20
Shift Operations – Try It Yourself Complete the following exercises: a. Given 101011, what will the value be after a left shift? b. Given hexadecimal value AF, what will the hexadecimal value be after a right shift of 3 places? c. How would the answer to (b) differ if the shift was an arithmetic shift? ( Answers on next slide)

21
Answers a) SHL 101011 010110 b) AF 16 1 0 1 0 1 1 1 1 2 SHR 3 0 0 0 1 0 1 0 1 2 15 16 c) AF 16 1 0 1 0 1 1 1 1 2 ASR 31 1 1 1 0 1 0 1 2 F5 16

22
Rotate Left operation ROL 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.ROL1 1 0 1 Ex 2. ROL1 1 0 0 = 1 0 0 1 1 0 11

23
Rotate Left – Multiple Bits ROL # bits means to rotate left # times Ex 1: ROL 31 0 0 1 1 1 0 0 = 1 1 1 0 0 1 0 0 Ex 2: ROL 21 1 1 0 0 1 1 0 = 1 0 0 1 1 0 1 1

24
Rotate Right operation ROR 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.ROR1 0 1 1 Ex 2.ROR0 0 1 1 = 1 0 0 1 1 0 11

25
Rotate Right – Multiple Bits ROR # bits means to rotate right # times Ex 1: ROR 31 0 0 1 1 1 0 0 = 1 0 0 1 0 0 1 1 Ex 2: ROR 21 1 1 0 0 1 1 0 = 1 0 1 1 1 0 0 1

26
Rotate Operations – Try It Yourself Complete the following exercises: a. Given 101011, what will the value be after a left rotation? b. Given hexadecimal value 2E, what will the hexadecimal value be after a left rotation of 5 places? c. How would the answer to (b) differ if the rotation was a right rotation? (Answers on next slide)

27
Answers a) ROL 101011 010111 b) 2E 16 0 0 1 0 1 1 1 0 2 ROL 5 1 1 0 0 0 1 0 1 2 C5 16 c) 2E 16 0 0 1 0 1 1 1 0 2 ROR 5 0 1 1 1 0 0 0 1 2 71 16

Similar presentations

© 2019 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google