pete said:
As far as getting an arithmetic shift goes, dividing by two
*does* get an arithmetic shift for negative integers, always.
Well, not exactly. On two's complement systems, -1 right-
shifted with sign propagation equals -1, but -1 / 2 is zero
(under C99 rules).
But, you just reminded me that some negative division itself,
what the arithmetic shift is, is implementation defined in C89.
ISO/IEC 9899: 1990
6.3.5 Multiplicative operators
If either operand is negative,
whether the result of the / operator is the
largest integer less than or equal to the algebraic quotient
or the smallest integer greater than
or equal to the algebraic quotient is implementation-defined,
as is the sign of the result of the % operator.
If the quotient a/b is representable,
the expression (a/b)*b + a%b shall equal a.
Yes: C90 permitted the implementation to choose, and
provided the div() and ldiv() functions when predictable
behavior was needed. C99 eliminated the choice, but div()
and ldiv() -- and now lldiv()! -- are still with us.
<off-topic excuse="intriguing trivia">
According to the Rationale, the choice was eliminated to
make C99 comply with Fortran's rules for arithmetic. Ponder
this next time somebody sneers about "dead languages" -- and
ponder, too, how some of C's weirdnesses are perpetuated in
Java. "But we've always done it that way" has more force
than we may like to admit.
</off-topic>