[fpc-devel] bitwise shift oddity a << b
Adriaan van Os
fpc at microbizz.nl
Tue May 19 14:27:22 CEST 2015
> Jonas Maebe wrote:
>
>> I would propose to document it as "undefined behaviour", just like C
>> does (http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf ,
>> section 4.5.7). The reason is that the behaviour can differ depending on
>
> Section 6.5.7. But in that standard the behaviour is undefined also when "the value of the right
> operand is greater than or equal to the width of the promoted left operand", which I think is a bit
> too simplistic (since, if we start talking about the maximum number of bits that can "legally" be
> shifted, then that number depends on the actual value of the left operand, but on the other hand if
> we accept that bits get lost, then there is no reason to limit the number of bits that can be shifted)
In other words - if we allow a 1-bit shift to be always correct (independent of the value of the
left operand) then N 1-bit shifts are always correct too (for any N >= 0). And then, introducing a
difference between shifting N bits and shifting Nx1 bits is absurd.
Not much thought went into that C-standard (nor in anything related to C).
Regards,
Adriaan van Os
More information about the fpc-devel
mailing list