[fpc-devel] Unexpected behaviour of bit operators

Marco Borsari borsa77 at libero.it
Fri May 17 14:36:52 CEST 2019

On Fri, 17 May 2019 11:51:20 +0100
"J. Gareth Moreton" <gareth at moreton-family.com> wrote:

> One thing to be aware of is that the compiler will extend intermediate 
> expressions to the CPU size, so if the multiplication overflows into 32 
> bits in h1 (which it does for the given values of a and b), it will 
> preserve those bits and will end up shifting them to the right instead 
> of zeroes.
> For h2, the overflowed bits are masked out with the "and ((s - 1) shl 
> (16 - n))" operation, which in this example is equal to $FFF0.
> I hope this answers your question.
> Gareth aka. Kit

Thank you, your answer make it clear the nature of the problem, i.e.
operation size extension.
Anyway, if I understand correct, the masking as reported in the code
does not operate over the 16 bit limit, so even h2 should be erroneus.
Simplex sigillum veri

More information about the fpc-devel mailing list