Logical and Shift operations

Download Report

Transcript Logical and Shift operations

Logical and Shift operations
A particular bit, or set of bits, within the
byte is 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 and shift
operations provide the means for
manipulating the bits.
Logical operations
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 operations
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
Logical operations
Ex 2:
11001001
OR 00001010
11001011
Ex 3:
0111
OR 0010
0111
Logical operations
XOR Operations

The exclusive OR. Similar to OR except that it
gives 0 when both its operands are 1.
Rules.
0
0
1
1
XOR
XOR
XOR
XOR
0=0
1=1
0=1
1=0
Logical operations

Ex 1:
10011001
XOR 00001111
10010110
Ex 2:
0111
XOR 0010
0101
Logical operations
NOT Operations

NOT is a separate operator for flipping all bits.
Rules.
NOT 0 = 1
NOT 1 = 0
Example.
NOT 1010
0101
Logical operations
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 operations
Ex 1:
11010011
AND 00001111
00000011
Ex 2:
0111
AND 1001
0001
Shift and Rotate operations
Where as 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 and Rotate operations
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
1 1010
Ex 2.
SHL 1100
1000
Shift and Rotate operations
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 1011
0101 1
Ex 2.
SHR 0011
0001
Shift and Rotate operations
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 1101
1011
ROL 1100
1001
Shift and Rotate operations
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 1011
1101
Ex 2.
ROR 0011
1001