I'm using SHR and SHL to convert fixed point <-> integers but I've find a problem.
As you see, SHR doesn't add the '1' bit. What can I use in the "fixed_to_integer" function?Code:program project2; FUNCTION integer_to_fixed (v: LONGINT): LONGINT; BEGIN integer_to_fixed := v SHL 16; END; FUNCTION fixed_to_integer (v: LONGINT): LONGINT; BEGIN fixed_to_integer := v SHR 16; END; VAR v: LONGINT; begin v := integer_to_fixed (-1); { Gets -1 in 16.16 fixed point. } WriteLn (v); { Writes the value as stored in memory. } WriteLn (fixed_to_integer (v)); { Should write '-1' but writes '65535'. } ReadLn; end.
[edit] Found a solution:
Is this the bets one?Code:FUNCTION fixed_to_integer (v: LONGINT): LONGINT; BEGIN IF v < 0 THEN fixed_to_integer := (v SHR 16) OR $FFFF0000 ELSE fixed_to_integer := v SHR 16; END;
Bookmarks