   Views: 932,816,631Time: 2021-10-22 03:20:29 AM 9 users online: ElderSubZero, FrozenQuills, JupiHornet, Kaching720, morisot, , SJandCharlieTheCat, TheReaper, Yoshimaster96 - Guests: 108 - Bots: 220 Users: 52,067 (2,105 active)Latest: Graul  Tip: Use the Iggy/Larry Battle Tools to edit Iggy/Larry's platform. Not logged in.    Difference Between Logical and Arithmetic Shift
Forum Index - Non-SMW Hacking - Misc. ROM Hacking - Difference Between Logical and Arithmetic Shift
 Pages: 1 Link
So guys I was wondering what is the difference between a logical shift and an arithmetic. Of course I barely have any coding skill so I'm not that knowledgeable. So is a logical shift just moving the bytes right or left? I don't know... I have no idea what an arithmetic shift does... I don't even know what arithmetic is! Because that's like high school level stuff and I have literally no knowledge on that, Cause the education systems I were in were utter dog shit.
Bit shifting can be best described as multiplying or dividing a value by 2. That means, if you move the bits to the left, you multiply the value by two and if you shift the bits to the right, it's practically a division by two. Except this isn't all true: The main difference between a logical and arithmetic shift is that the former treats the value unsigned and the latter signed.
This has got no difference for left shifts where arithmetic and logical left shifts are virtually if not actually identical but there is a notable difference in right shifts, namely that the logical right shift is simply the right shift equivalent to left shifts —the highest bit is always zero after the operation— whereas with arithmetic right shifts, the highest bit is never cleared, thus giving the appearance of a signed division by two (the only exception is -1 in which the value stays -1 instead of 0).

To see what I mean, let's compare two byte values: 0x42 and 0xE5. 0x42 is 0b01000010 in binary whereas 0xE5 is 0b11100101. Since both left shifts are the same, I don't have to bring in a comparison so let's just focus on LSR (logical shift right) and ASR (arithmetic shift right):
LSR 0x42 = LSR 0b01000010 = 0b00100001 = 0x21
ASR 0x42 = ASR 0b01000010 = 0b00100001 = 0x21
LSR 0xE5 = LSR 0b11100101 = 0b01110010 = 0x72
ASR 0xE5 = ASR 0b11100101 = 0b11110010 = 0xF2
See how shifting 0x21 to the right is identical but 0xE5 is different? That's where arithmetic and logical shifts differ.

 Pages: 1 Link       