SHR & SHL "normal" behaviors

Language reference ›› Statements ›› Assignments and expressions ››
Parent Previous Next

As in standard Pascal, SHR and SHL applied to signed numbers implies an automatic cast to unsigned, so shifts are always "logical" - never "arithmetic".

This is Pascal standard. Period.

This explanation is to anticipate discussions on this subject.

The C standard says there is no standard for >> and << operators on signed numbers: this is left "implementation dependent"; the consequence is that their behaviors differ between compilers and the subject generates long discussions in forums and discussion groups...

Note that the PMP optimizer engine is smart enough to use "logical shifts" or "arithmetic shifts" when a unsigned or a signed number DIV by a power of two is required. Direct use of SHR will not improve the code efficiency. PMP also uses SHL to multiply by a power of two in some circumstances.

Also, as defined in the promotion rules, shifting a 8-bit expression gives a 8-bit result, so:

<16-bit variable> := <8-bit expression> SHL 8; will assign zero.


<16-bit variable> := <8-bit expression> * 256; will do a 16-bit shift left of 8 positions, as 256 is a 16-bit expression.