[fpc-pascal]I don't understand value typecast

Jonas Maebe jonas at zeus.rug.ac.be
Tue Jul 2 16:11:50 CEST 2002


On zondag, juni 30, 2002, at 11:25 , Luis Del Aguila wrote:

> In the manual explain that the value typecast doesn't work, when the 
> type
> size of the expression and the size of the type cast  not must be the 
> same.
> (page 58. ref.pdf)
> But this program compile.
>
> Var a:integer;
>       b:char;
>       c:boolean;
>       d:word;
> Begin
>    a:=integer('A');
>    b:=Char(4875);
>    c:=boolean(100);
> End.
>
> How work the value typecast?

I think the manual is incomplete. For ordinal-to-ordinal type casts 
(integers/chars/booleans/range-types) and possibly also 
ordinal-to-enumerations and vice versa, this same-size requirement 
doesn't exist. In such a case,

a) when going from a smaller signed type to a larger signed type (e.g. 
longint_var := longint(shortint_var)), the effect is the same as when 
you would do a regular assignment (so if shortint_var contains $ff = -1 
above, longintvar will become $ffffffff = -1)
b) when going from a smaller unsigned type to any larger type (e.g. 
longint_var := longint(byte_var)), the value of the smaller type is 
simply zero-extended (so if byte_var contains $ff = 255 above, 
longint_var will become $ff = 255)
c) when going from a larger to a smaller type, you just take the n lower 
bytes of the larger type and copy them into the smaller type, where n = 
the size in bytes of the smaller type.

Char, Boolean and all enumeration types (which don't have an explicit 
lower bound set that's negative) are treated as unsigned.


Jonas





More information about the fpc-pascal mailing list