Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

W1024 Combining signed and unsigned types - widened both operands 1

Status
Not open for further replies.

pierrotsc

Programmer
Nov 25, 2007
358
US
I have this warning but really do not know on how to fix that.
The source compiles fine. i just would like to clear the warning.
Thanks.
PO
 
from delphi help:

Code:
To mathematically combine signed and unsigned types correctly the compiler must promote both operands to the next larger size data type and then perform the combination.
 
To see why this is necessary, consider two operands, an Integer with the value -128 and a Cardinal with the value 130. The Cardinal type has one more digit of precision than the Integer type, and thus comparing the two values cannot accurately be performed in only 32 bits. The proper solution for the compiler is to promote both these types to a larger, common, size and then to perform the comparison.
 
The compiler will only produce this warning when the size is extended beyond what would normally be used for calculating the result.
 
{$APPTYPE CONSOLE}
program Produce;
  var
    i : Integer;
    c : Cardinal;

begin
  i := -128;
  c := 130;
  WriteLn(i + c);
end.

    
In the example above, the compiler warns that the expression will be calculated at 64 bits rather than the supposed 32 bits.

makes sense?

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
yes it does. so i guess i should change my integer variable to a cardinal to make the warning disappear. Am i correct?

Thank you.
PO
 
yes, but it depends on your code, you could have the same issue mixing signed and unsigned operands.

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
so i guess i should change my integer variable to a cardinal to make the warning disappear. Am i correct?
"Correct" depends on whether your variable can go negative. If not (and you should error-trap for this) then Cardinal works. If you expect to work with negative numbers, then you should use Int64, which is the type that the compiler was promoting to.

Note that in the example given by whosrdaddy, you could not use
Code:
var i : Cardinal;
because a negative number will be assigned to i.
 
This more strict maths warning was introduced between versions 4 and 7, I got several of them after upgrading!
If you do anything about it is up to you as the compiler has attempted to 'sort it out' for you.


Steve: N.M.N.F.
If something is popular, it must be wrong: Mark Twain
 
It is a warning that the two have the *potential* of one being outside the range of the other.

Roo
Delphi Rules!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top