Date post: | 19-Dec-2015 |
Category: |
Documents |
View: | 225 times |
Download: | 1 times |
Logical, Shift, and Rotate Operations
CS208
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.
Logical OR RulesOR Operations
OR Results in 1 if either or both of the operands are 1.
OR Table
0 OR 0 = 00 OR 1 = 11 OR 0 = 11 OR 1 = 1
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
OR 0 0 0 0 1 1 1 1
1 0 0 1 1 1 1 1
Logical OR Examples
Ex 3: 0 1 1 1
OR 0 0 1 0 0 1 1 1
Ex 2: 1 1 0 0 1 0 0 1
OR 0 0 0 0 1 0 1 0
1 1 0 0 1 0 1 1
Logical XOR RulesXOR Operations The exclusive OR. Similar to OR except that it
now gives 0 when both its operands are 1.
Rules.0 XOR 0 = 00 XOR 1 = 11 XOR 0 = 11 XOR 1 = 0
Logical XOR Examples
Ex 1: 1 0 0 1 1 0 0 1
XOR 0 0 0 0 1 1 1 1
1 0 0 1 0 1 1 0
Ex 2: 0 1 1 1
XOR 0 0 1 0
0 1 0 1
Logical AND Rules
AND Operations
AND yields 1 only if both its operands are 1.
Rules.0 AND 0 = 00 AND 1 = 01 AND 0 = 01 AND 1 = 1
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
Logical NOT
NOT Operations
NOT is a separate operator for flipping the bits.
Rules.NOT 0 = 1NOT 1 = 0
Example. NOT 1 0 1 0 = 0 1 0 1
Logical Operations –Try It Yourself
Complete the following exercises:
1 0 0 1 1 0 0 1 OR 0 1 0 1 AND 0 1 0 1
1 0 0 1 XOR 0 1 0 1 NOT 10010111 = (Answers on next slide)
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
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.
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. SHL 1 1 0 1
Ex 2. SHL 1 1 0 0 = 1 0 0 0
01 1 0 11 1 0 1 = 1 0 1 0
Shift Left - Multiple Bits SHL # bits means to shift left # times
Ex 1: SHL 3 1 0 0 1 1 1 0 0
Ex 2: SHL 2 1 1 1 0 0 1 1 0
= 1 0 0 1 1 0 0 0
1 0 0 1 1 1 0 01 0 0 1 1 1 0 0 0 0 0 = 1 1 1 0 0 0 0 0
Shift Right operationSHR
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. SHR 1 0 1 1
Ex 2. SHR 0 0 1 1 = 0 0 0 1
0 1 0 1 1 = 0 1 0 1
Shift Right – Multiple Bits
SHR # bits means to shift right # times
Ex 1: SHR 3 1 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 2 1 1 1 0 0 1 1 0
= 0 0 1 1 1 0 0 1
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
Ex 2. ASR 0 0 1 1 = 0 0 0 1
1 1 0 1 1 = 1 1 0 1
Arithmetic Shift Right – Multiple Bits
ASR # bits means to arithmetic shift right # times
Ex 1: ASR 3 1 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 2 0 1 1 0 0 1 1 0
= 0 0 0 1 1 0 0 1
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)
Answers
a) SHL 101011 010110
b) AF16 1 0 1 0 1 1 1 12
SHR 3 0 0 0 1 0 1 0 12 1516
c) AF16 1 0 1 0 1 1 1 12
ASR 3 1 1 1 1 0 1 0 12 F516
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. ROL 1 1 0 1
Ex 2. ROL 1 1 0 0 = 1 0 0 1
1 0 1 1
Rotate Left – Multiple Bits ROL # bits means to rotate left # times
Ex 1: ROL 3 1 0 0 1 1 1 0 0
= 1 1 1 0 0 1 0 0
Ex 2: ROL 2 1 1 1 0 0 1 1 0
= 1 0 0 1 1 0 1 1
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. ROR 1 0 1 1
Ex 2. ROR 0 0 1 1 = 1 0 0 1
1 0 11
Rotate Right – Multiple Bits ROR # bits means to rotate right # times
Ex 1: ROR 3 1 0 0 1 1 1 0 0
= 1 0 0 1 0 0 1 1
Ex 2: ROR 2 1 1 1 0 0 1 1 0
= 1 0 1 1 1 0 0 1
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)
Answers
a) ROL 101011 010111
b) 2E16 0 0 1 0 1 1 1 02 ROL
5 1 1 0 0 0 1 0 12 C516
c) 2E16 0 0 1 0 1 1 1 02
ROR 5 0 1 1 1 0 0 0 12 7116