Logical Right Shift vs Arithmetic Right Shift

In binary computing, right shift operations move bits to the right by a specified number of positions, but they differ fundamentally in how they handle the vacated bits at the most significant bit (MSB) position. A logical right shift always fills the leftmost empty positions with zeros, making it suitable for unsigned numbers and raw bit manipulation. In contrast, an arithmetic right shift duplicates the original sign bit (the MSB) to preserve the sign of signed integers represented in two’s complement, allowing for accurate division by powers of two.

Logical Right Shift

A logical right shift shifts every bit of a binary sequence to the right by a defined number of places. The bits shifted off the right end (the least significant bits) are discarded, and the vacant spaces on the left (the most significant bits) are always replaced with 0.

Example (8-bit binary shifted right by 1): * Initial binary: 1100 1000 (200 in unsigned decimal) * Shifted: 0110 0100 (100 in unsigned decimal)

Arithmetic Right Shift

An arithmetic right shift also moves all bits to the right, but it retains the original sign of the number by using a technique called sign extension. The leftmost vacant positions are filled with whatever value was originally in the most significant bit—0 for positive numbers or 1 for negative numbers in two’s complement representation.

Example 1: Positive Number (8-bit binary shifted right by 1): * Initial binary: 0100 1000 (+72 in decimal) * Shifted: 0010 0100 (+36 in decimal)

Example 2: Negative Number (8-bit binary shifted right by 1): * Initial binary: 1100 1000 (-56 in two’s complement) * Shifted: 1110 0100 (-28 in two’s complement)

Key Differences Summary

Feature Logical Right Shift Arithmetic Right Shift
Bit Filling Rule Always fills the left with 0. Copies the original MSB (0 or 1).
Primary Use Case Unsigned integers and bitmask operations. Signed two’s complement integers.
Sign Preservation Does not preserve negative signs. Preserves negative and positive signs.
Common Operators >>> (JavaScript, Java) >> (C, C++, Java, JavaScript, Python)