What is Arithmetic shift?

A right shift of a signed integer that copies the sign bit into the vacated high bits. Shifting a negative number right by one divides by 2, rounding toward negative infinity. Filling with zeros is a logical shift.

Field: Computer Arithmetic

Japanese name: 算術シフト

How an arithmetic right shift works

An arithmetic right shift moves the bits of a two's-complement signed integer to the right and copies the original most significant bit (the sign bit) into the vacated high bits. Positive numbers get 0s and negative numbers get 1s, so the sign is preserved. For example, shifting 8-bit −8 (1111 1000) right by one gives 1111 1100, which is −4. An n-bit arithmetic right shift equals the original value divided by 2 to the n, rounded downward (toward negative infinity). A logical right shift, which inserts 0s at the top, is meant for unsigned integers; applied to a negative number it produces a large positive one.

Division and rounding direction

Because an arithmetic right shift rounds toward negative infinity, it can give a different result from division that truncates toward zero when the value is negative. In C (since C99), −7 / 2 is −3, but arithmetic-shifting −7 (1111 1001) right by one gives 1111 1100, which is −4. So when a compiler replaces division of a signed integer by 2 to the n with a shift, it adds correction instructions, such as first adding 2 to the n minus 1 when the value is negative and then shifting. For unsigned integers or values known to be non-negative, the shift can replace the division without correction.

Arithmetic left shift and overflow

For left shifts, both arithmetic and logical shifts insert 0s at the bottom, so the bits move the same way; that is why x86's SAL and SHL instructions behave identically. The value is multiplied by 2 to the n, but for signed integers, if the bits shifted out or the new top bit differ from the original sign, overflow occurs, the value is wrong, and the sign may change. For example, shifting 8-bit 64 (0100 0000) left by one gives 1000 0000, which is −128. Note that in C, left-shifting a negative value, or left-shifting a signed integer so the result does not fit the type, is undefined behavior.

Instruction sets and hardware

Most instruction sets provide arithmetic and logical right shifts as separate instructions: SRA/SRAI and SRL/SRLI in RISC-V, ASR and LSR in Arm, and SAR and SHR in x86. Among programming languages, Java distinguishes >> as the arithmetic right shift and >>> as the logical one. In hardware, a barrel shifter supports both simply by switching whether the bits fed in at the top are the sign bit or 0. Fixed-point DSP code uses arithmetic right shifts to scale values down, but because the rounding is biased toward negative values, 2 to the (n−1) is sometimes added first to approximate round-to-nearest.

Try it: questions about Arithmetic shift

In C, what are the results of −7 / 2 and −7 >> 1 (with an arithmetic shift) for int?

  1. A−7 / 2 is −3, and −7 >> 1 is −4
  2. BBoth are −4
  3. C−7 / 2 is −4, and −7 >> 1 is −3
Show answer and explanation

Answer: A. −7 / 2 is −3, and −7 >> 1 is −4

C defines integer division as truncating toward zero, so −3.5 becomes −3. An arithmetic right shift instead rounds toward negative infinity: shifting −7 (1111 1001) right by one gives 1111 1100 = −4. For negative numbers, shifting and dividing therefore disagree, so when compilers replace division by 2 with a shift they add correction instructions for negatives. For positive numbers the two agree.

FAQ

What is the difference between an arithmetic shift and a logical shift?

In a right shift, an arithmetic shift copies the sign bit into the vacated high bits, while a logical shift inserts 0s. Arithmetic shifts are used for signed integers, logical shifts for unsigned integers and bit manipulation. In a left shift, both insert 0s at the bottom, so the bits move the same way.

Is an arithmetic right shift the same as dividing by 2?

For positive numbers yes, but for negative numbers it can differ. An arithmetic right shift rounds toward negative infinity, so −7 becomes −4, whereas C's division, which truncates toward zero, gives −3.

Is C's >> an arithmetic shift?

For unsigned integers and non-negative signed integers, the result is defined to equal division by a power of two. Right-shifting a negative signed integer is implementation-defined; most compilers use an arithmetic shift, but portable code is safer not relying on it.

Books on Computer Arithmetic Ad

As an Amazon Associate I earn from qualifying purchases.

Semiconductor, Electronics and Computing Calculators

This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.

Back to the glossary