Logical, Shift, and Rotate Operations CS208. Logical, Shift and Rotate Operations A particular bit,...

27
Logical, Shift, and Rotate Operations CS208
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    223
  • download

    1

Transcript of Logical, Shift, and Rotate Operations CS208. Logical, Shift and Rotate Operations A particular bit,...

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

Logical, Shift, and Rotate Operations

CS208

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

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.

Page 3: 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.

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

Page 4: 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.

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

Page 5: 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.

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

Page 6: 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.

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

Page 7: 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.

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

Page 8: 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.

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

 

Page 9: 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.

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

Page 10: 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.

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

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

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)

Page 12: 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.

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

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

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.

Page 14: 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.

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

Page 15: 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.

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

Page 16: 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.

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

Page 17: 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.

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

Page 18: 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.

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

Page 19: 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.

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

Page 20: 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.

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) 

Page 21: 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.

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

Page 22: 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.

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

Page 23: 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.

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

Page 24: 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.

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

Page 25: 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.

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

Page 26: 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.

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)  

Page 27: 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.

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