FSMaxB
FSMaxB

Reputation: 2490

Is rightshifting signed int by 8 * sizeof(int) or more undefined?

I know that this is undefined:

uint32_t u = 1;
u << 32;

But I'm a little bit confused about which kinds of shifts are undefined.

Is it undefined to shift a signed integer by it's size (in bits) or more to the right?

Update: As pointed out in the answer, this is about the size in bits, not in bytes.

Upvotes: 4

Views: 125

Answers (3)

supercat
supercat

Reputation: 81189

The C language was in wide use before the C89 Standard was written, and the authors of the C89 Standard did not want to impose any requirements for corner-case semantics which might be contrary to anything that existing implementations were already doing.

Some implementations will behave badly when given a very large or negative shift amount (e.g. I think on a Transputer a shift of -1 will take about 4294967295 clock cycles to execute, during which time interrupts will be disabled), and some implementations when asked to shift by the word length won't shift at all. The writers of the Standard saw no reason to distinguish between those behaviors, and simply regarded the word size as the threshold beyond which it would impose no requirements.

Although the proper meaning for negative values is unclear when using formats other than two's-complement, there is no real ambiguity with two's-complement values beyond the fact that some existing implementations use logical right- shift rather than arithmetic right-shift even with signed types and the Committee didn't want to mandate that any existing compilers change behavior that code might be relying upon it. When using negative values in formats other than two's-complement, it's unclear what shifts in either direction should mean, but the Committee thought it more likely that there might exist a machine that would do something weird when left-shifting a negative number than that a machine might do something weird when right-shifting a negative number.

Note that the notion that a quality compiler given something like:

unsigned long rotate_left(unsigned long dat, int amount)
{ return (dat << amount) | (dat >> (32-amount)); }

should do anything other than yield "dat" when amount==0 [note that the two common ways of evaluating the right-shift would yield the same result] is comparatively recent. I think the authors of C89 intended that if quality compilers for platform X could be expected to behave a certain way before C89 was published, and such behavior would be allowable under the Standard, quality compilers for that platform should continue to behave that way. Nonetheless, an attitude has emerged that programmers should not be allowed to rely upon any behaviors not mandated by the Standard, even on platforms where they would be useful and cheap.

Upvotes: 0

Eugene Sh.
Eugene Sh.

Reputation: 18351

Everything in the standard. The Section 6.5.7p3

3) 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.

This applies for both left and right shifts, and for both signed and unsigned operands on the left side of the operator. There are additional constraints on shifting signed operands.

Upvotes: 2

Keith Thompson
Keith Thompson

Reputation: 263337

sizeof (int) is the size of int in bytes, so it's not relevant. What's relevant is not the size, but the width, which is the number of value bits in the representation (plus the sign bit for signed types).

If the right operand of a << or >> operator is greater than or equal to the width of the promoted left operand, the behavior is undefined. (For example, if the left operand is of type short, it's promoted to int before the operation is applied).

For the << left-shift operator, the behavior is defined only if the left operand is non-negative and the result is representable.

For the >> right-shift operator, the result is implementation-defined if the left operand is negative.

This is all defined in section 6.5.7 of the C standard (the link is to N1570, the most recent publicly available C11 draft).

Here's the full description of the semantics:

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 × 2E2, reduced modulo one more than the maximum value representable in the result type. If E1 has a signed type and nonnegative value, and E1 × 2E2 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 / 2E2. If E1 has a signed type and a negative value, the resulting value is implementation-defined.

Upvotes: 6

Related Questions