Keith Thompson <(E-Mail Removed)> writes:

[...]

> The standard imposes some fairly stringent requirements on how

> integer, both signed and unsigned, are represented. These

> requirements are tight enough to guarantee (I think) that the

> "multiple or divide by powers of 2" and "shift by N bits" models of

> the "<<" and ">>" operators turn out to be equivalent (with the

> proviso that any padding bits don't participate).
Since not everyone has a copy of the standard, here's what C99 says in

its description of the bitwise shift operators. (I've replaced the

multiplication symbol 'x' with "*", and the superscript exponent with

a "**" operator.)

The integer promotions are performed on each of the operands. The

type of the result is that of the promoted left operand. If the

value of the right operand is negative or is greater than or equal

to the width of the promoted left operand, the behavior is

undefined.

The result of E1 << E2 is E1 left-shifted E2 bit positions;

vacated bits are filled with zeros. If E1 has an unsigned type,

the value of the result is E1 * 2**E2, reduced modulo one more

than the maximum value representable in the result type. If E1 has

a signed type and nonnegative value, and E1 * 2**E2 is

representable in the result type, then that is the resulting

value; otherwise, the behavior is undefined.

The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1

has an unsigned type or if E1 has a signed type and a nonnegative

value, the value of the result is the integral part of the

quotient of E1 / 2**E2. If E1 has a signed type and a negative

value, the resulting value is implementation-defined.

--

Keith Thompson (The_Other_Keith)

(E-Mail Removed) <http://www.ghoti.net/~kst>

San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>

We must do something. This is something. Therefore, we must do this.