If you're on a two's-complement CPU (i.e. any modern computer) and you
use a reasonably modern compiler, you have nothing to fear...
right-shift will sign-extend. The C standard doesn't require it because
when the standard was written, there were still some one's-complement
CPUs in existence.
Honestly, most people's code would have huge numbers of errors if we
ever decided to port it to a one's-complement CPU anyway, so I wouldn't
worry about it. If you rely on signed right-shifts, your code WILL work
on Intel, PowerPC, 68K, MIPS, and any other CPU that you might want to
use. It won't work on a computer from the 60s, but there are probably
20 other valid reasons why it won't work on that 60s computer as well.
On Oct 22, 2004, at 8:05 AM, Holger Bettag wrote:
There is still a portability problem with the shift,
though, because AFAIK there is no guarantee that rightshifts do the
expected thing with the sign bit on every platform (i.e. some machines
might not implement both signed and unsigned shift primitives, but I
think all do nowadays).