Couperin wrote:
I discovered a small bug in GPC FAQ (Reference : shr) :
It give as an example : "a:= -127 shr 4; (* yields -7 *)" which is
false, it yields -8 !
I think we have to take care when manipulating bits of negative
integers.
A ShortInt with value-1 is coded 11111111b ! (-2 is 11111110b, -3 is
11111101b and so on)
If the last bit is 1 (that means the number is negative), the 'shr'
operator replaces the right new bits by 1.
Examples (Considering ShortInt type (8 bits)) :
-127 (coded 10000001b) shr 4 yields -8 (coded 11111000b)
-1 (coded 11111111b) shr 1 yields -1 !
You're right, `shr 4' is different from `div 16' in that `div'
rounds towards minus infinity while `div' round towards 0.
I'm fixing it in the manual (the compiler's behaviour is correct),
thanks for the hint.
Frank
--
Frank Heckenbach, frank@g-n-u.de,
http://fjf.gnu.de/
GPC To-Do list, latest features, fixed bugs:
http://agnes.dida.physik.uni-essen.de/~gnu-pascal/todo.html