Semi constant expressions

Mark Brader msb at sq.sq.com
Fri Sep 1 03:56:52 AEST 1989


>	x << (100000000L)

Unlike the 0*x and 0/x examples, this one is explicitly undefined behavior
(assuming that x is of an integral type less than 100000000 bits wide!).

See 3.3.7:
	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.

The reason for this restriction is to simplify implementation on computers
where the shift instruction's amount-to-shift field is only lg2(wordsize)
bits long -- that is, is only 5 bits if ints are 32 bits, for example --
or where only that many bits are actually examined, or where a software
shift is done and has similar restrictions.

By the way, the L on the right operand has no effect.  In the proposed
standard the type of the result of << is the promoted type of the left
operand, so it doesn't matter whether the right operand is int or long,
and the plain constant 100000000 would be one or the other depending on the
size of ints.


-- 
Mark Brader		    "...most mistakes are made the last thing before
SoftQuad Inc., Toronto	        you go to bed.  So go to bed before you do
utzoo!sq!msb, msb at sq.com	the last thing."	-- David Jacques Way

This article is in the public domain.



More information about the Comp.std.c mailing list