[fpc-pascal]runtime 201 with swapword function

Florian Klaempfl Florian.Klaempfl at gmx.de
Fri Jul 25 16:30:35 CEST 2003


James Mills wrote:
> On Fri, Jul 25, 2003 at 04:21:36PM +0200, Jonas Maebe wrote:
> 
>>On vrijdag, jul 25, 2003, at 16:22 Europe/Brussels, James Mills wrote:
>>
>>
>>>Why does the following result in a runtime error ?
>>>
>>>program example;
>>>
>>>const
>>>	THE_PORT = 9999;
>>>
>>>function swapWord(w: Word): Word;
>>>begin
>>>	swapWord := (w SHL 8) OR (w SHR 8);
>>>end;
>>
>>In Pascal, all calculations are performed using the base type (which is 
>>longint in FPC), so if w > 255, then w shl 8 > high(word) and will 
>>result in a range check error when assigning to swapWord. Even if we 
>>calculated everything using words, you'd still get an overflow error, 
>>this time when doing the shl.
> 
> 
> How can I correct this ?
> This bit of code was taken directly from server.pp in one of the old
> 2002 mailing threads...

Turn range checking off for this piece of code or do

function swapWord(w: Word): Word;
   begin
      swapWord := ((w SHL 8) OR (w SHR 8)) and $ffff;
   end;





More information about the fpc-pascal mailing list