57

I've been reading the classic Hacker's delight and I am having trouble understanding the difference between logical shift right,arithmetic shift right, and rotate right. Please excuse if the doubt seems too simple.

Also why is there no arithmetic shift left?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Chandrahas Aroori
  • 955
  • 2
  • 14
  • 27

3 Answers3

122

First remember that machine words are of fixed size. Say 4, and that your input is:

+---+---+---+---+
| a | b | c | d |
+---+---+---+---+

Then pushing everything one position to the left gives:

+---+---+---+---+
| b | c | d | X |
+---+---+---+---+

Question what to put as X?

  1. with a shift put 0
  2. with rotate put a

Now push everything one position to the right gives:

+---+---+---+---+
| X | a | b | c |
+---+---+---+---+

Question what to put as X?

  1. with a logical shift put 0
  2. with an arithmetic shift put a
  3. with rotate put d

Roughly.

Logical shift correspond to (left-shift) multiplication by 2, (right-shift) integer division by 2.

Arithmetic shift is something related to 2's-complement representation of signed numbers. In this representation, the sign is the leftmost bit, then arithmetic shift preserves the sign (this is called sign extension).

Rotate has no ordinary mathematical meaning, and is almost an obsolete operation even in computers.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
  • 1
    "Shift" could be more specifically referred to as "logical shift", no? – endolith Dec 05 '18 at 00:19
  • 2
    Logical shift does not divide and multiply by 2. For example, -75 >>> 1 = 90. The arithmetic shift preserves the sign, thus multiplying and dividing by 2 – Jed Aug 22 '19 at 14:37
  • 1
    I never said taht logical shift are for 2's complement numbers, only arithmetic shift. – Jean-Baptiste Yunès Aug 27 '19 at 15:43
  • 2
    Useful to note that arithmetic right shift rounds towards `-Infinity` while ordinary signed division (in C) truncated towards 0. So compiling `x/2` for signed x requires some sign-bit trickery on top of an SAR instruction. – Peter Cordes Aug 30 '19 at 11:48
  • 1
    The definition of Logical shift and Arithmetic shift at the end is incorrect. The arithmetic shift is the one that's used to divide or multiply by 2, not the Logical shift. Please correct. – Argus Waikhom Jan 18 '22 at 04:29
23

The difference is pretty much explained in the right-most column.

  • Logical shift treats the number as a bunch of bits, and shifts in zeros. This is the >> operator in C.
  • Arithmetic shift treats the number as a signed integer (in 2s complement), and "retains" the topmost bit, shifting in zeros if the topmost bit was 0, and ones if it was one. C's right-shift operator has implementation-defined behavior if the number being shifted is negative.

    For example, the binary number 11100101 (-27 in decimal, assuming 2s complement), when right-shifted 3 bits using logical shift, becomes 00011100 (decimal 28). This is clearly confusing. Using an arithmetic shift, the sign bit would be kept, and the result would become 11111100 (decimal -4, which is about right for -27 / 8).

  • Rotation does neither, since topmost bits are replaced by lowermost bits. C does not have an operator to do rotation.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • 1
    Can you explain arithmatic shift a little more clearly. And example please? – Chandrahas Aroori Jun 22 '17 at 09:12
  • @ChandrahasAroori there are tons of examples that you can find on google https://en.wikipedia.org/wiki/Bitwise_operation – phuclv Jun 22 '17 at 09:14
  • "11111100 (decimal ~4)" .. isn't that negative of 00000011 or -3 instead ? – KMC Jul 12 '20 at 00:08
  • @KMC This is 2's complement notation. In order to get 2's complement representation of -4, take the bitwise representation of 4, flip all the bits and add 1. – Hari Jun 14 '23 at 11:42
13

Logical right shift means shifting the bits to the right and MSB(most significant bit) becomes 0.

Example: Logical right shift of number 1 0 1 1 0 1 0 1 is 0 1 0 1 1 0 1 0.

Arithmetic right shift means shifting the bits to the right and MSB(most significant bit) is same as in the original number.

Example: Arithmetic right shift of number 1 0 1 1 0 1 0 1 is 1 1 0 1 1 0 1 0.

Tanu Arora
  • 231
  • 2
  • 7