Forum > General

What are the rules for signed and unsigned integers?

(1/1)

Artlav:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---constd=18446744073709551615;e=$FFFFFFFFFFFFFFFF;...writeln(d);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:
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"

Artlav:
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:
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.



 

Navigation

[0] Message Index

Go to full version