Recent

Author Topic: What are the rules for signed and unsigned integers?  (Read 3168 times)

Artlav

  • New Member
  • *
  • Posts: 36
    • Orbital Designs
What are the rules for signed and unsigned integers?
« on: November 04, 2017, 12:50:23 am »
I'm using FPC (with -Mdelphi) and am trying to get a formal idea on how do the signed and unsigned types interact, and how operand sizes are figured out.

Let's u1 and u2 are unsigned (dword or cardinal).
How does
Code: Pascal  [Select][+][-]
  1. writeln((10-u1) div 10);
get evaluated?
Should it see an unsigned parameter and treat the whole thing as unsigned, or narrow it down to signed because a - sign can flip the expression to negative?

Given
Code: Pascal  [Select][+][-]
  1. const
  2. d=18446744073709551615;
  3. e=$FFFFFFFFFFFFFFFF;
  4. ...
  5. writeln(d);
  6. writeln(e);
I'm getting unsigned value for d, but signed (-1) value for e.
What determines that?

I guess the total question would be how does the compiler determines whether each step of an expression is a signed operation or not?

Eugene Loza

  • Hero Member
  • *****
  • Posts: 663
    • My games in Pascal
Re: What are the rules for signed and unsigned integers?
« Reply #1 on: November 04, 2017, 07:02:18 am »
See "automatic type conversion" here https://www.freepascal.org/docs-html/ref/refsu4.html (thanks to @Leledumbo for the link)
That means that
"Word - 10" is "integer"
"DWord - 10" is "int64"
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

Artlav

  • New Member
  • *
  • Posts: 36
    • Orbital Designs
Re: What are the rules for signed and unsigned integers?
« Reply #2 on: November 04, 2017, 11:09:21 am »
Ok, that clears things, even if it sounds odd - i kept almost posting questions, but applying the rules exactly as written always solved the case.
Thanks!

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: What are the rules for signed and unsigned integers?
« Reply #3 on: November 21, 2017, 11:50:06 pm »
Maybe I can add something to this..

 The most significant bit of the type when set turns it into a Signed (-)
 
  If was to type cast a value like this...

  MyDWord := DWord(MyInteger); it would just turn it into a DWORD, basically
 nothing has changed other than the compiler getting fooled.

 So a -1 would be the same as a Dword fully on..
Now do that with Range Checking on..

 a Range check triggers an Error if a -1 was to be moved to a Dword,  although just
fine if you know what to expect.



 
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018