# Logical, Shift, and Rotate Operations by d6p9V2

VIEWS: 69 PAGES: 27

• pg 1
```									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 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
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 0 0 0 0 1 1 1 1
10011111
Logical OR Examples
Ex 2:      11001001
OR 0 0 0 0 1 0 1 0
11001011

Ex 3:      0111
OR 0 0 1 0
0111
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
Logical XOR Examples
Ex 1:       10011001
XOR 0 0 0 0 1 1 1 1
10010110

Ex 2:            0111
XOR 0 0 1 0
0101
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
Logical AND Examples

Ex 1:       11010011
AND 0 0 0 0 1 1 1 1
00000011

Ex 2:       0111
AND 1 0 0 1
0001
Logical NOT
NOT Operations
   NOT is a separate operator for flipping the bits.
Rules.
NOT 0 = 1
NOT 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 =

   OR       1101

   AND      0001

   XOR      1100

   NOT      01101000
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    1101
010
11 1 0 1     =1010

Ex 2.       SHL    1100
=1000
Shift Left - Multiple Bits
   SHL # bits means to shift left # times

Ex 1:     SHL 3    10011100
1100000
1 0 01 0 0 1 1 1 0 0 = 1 1 1 0 0 0 0 0

Ex 2:     SHL 2    11100110
=10011000
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.    SHR 1 0 1 1
0101 1         =0101

Ex 2.    SHR 0 0 1 1
=0001
Shift Right – Multiple Bits
   SHR # bits means to shift right # times

Ex 1:     SHR 3       10011100
00010011 100 =00010011

Ex 2:     SHR 2      11100110
=00111001
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            =1101

Ex 2.      ASR 0 0 1 1
=0001
Arithmetic Shift Right –
Multiple Bits

   ASR # bits means to arithmetic shift right # times

Ex 1:     ASR 3       10011100
11110011 100 =11110011

Ex 2:     ASR 2      01100110
=00011001
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?
   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
1011

Ex 2.       ROL    1100
=1001
Rotate Left – Multiple Bits
   ROL # bits means to rotate left # times

Ex 1:     ROL 3    10011100
=11100100

Ex 2:     ROL 2    11100110
=10011011
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
11 0 1

Ex 2.       ROR 0 0 1 1
=1001
Rotate Right – Multiple Bits
   ROR # bits means to rotate right # times

Ex 1:    ROR 3    10011100
=10010011

Ex 2:     ROR 2 1 1 1 0 0 1 1 0
=10111001
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?