Shifting question

Guy Harris guy at gorodish.Sun.COM
Tue Jul 19 04:54:10 AEST 1988


>  I would certainly think so. The action of a shift on an unsigned is
> pretty well defined,

Yes, but the action of a shift *whose magnitude is equal to the number of bits
in the operand* is not well defined by either K&R or by the January 11, 1988
ANSI C draft.  K&R:

	7.5 Shift operators

	...

	The result is undefined if the right operand is negative, or greater
	than or equal to the length of the object in bits.

ANSI C:

	3.3.7 Bitwise shift operators

	...If the value of the right operand is negative or is greater than or
	equal to the width in bits of the promoted left operand, the behavior
	is undefined.

> x>>32 should work the same on any machine, 8, 16, 32, or 64 bits. Also, the
> action of doing:
> 	x >>= 16; x>>= 16;
> better be the same as:
> 	x = x>>32;

Perhaps they *should*; however, neither the K&R nor the January 11 ANSI C draft
specifications for the language require this.  If you think that they should, I
suggest you lobby the ANSI C committee.



More information about the Comp.lang.c mailing list