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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ulong confusion...

Status
Not open for further replies.

stationX

Technical User
Jan 11, 2008
22
GB
Hi All,
Can anyone explain the behaviour of the following code?

//ulong test1 = 5555555555555555555 * 1000;
ulong test1 = 5555555555555555555;
test1 = test1 * 1000;
Console.WriteLine(test1);

(refering to the comment out line) If you try to create the variable from a huge number * 1000 the line gets flagged in visual studio saying it will overload (as you'd expect).
But if the variables is created and then multiplyed by 1000 using another statement no errors or warnings occur.
The result is not as expected but it is displayed without errors.

The reason I ask is that I am trying to modify some code that uses the above process as an integral part of its operation. This suggests to me it is probably normal behaviour that I'm not aware of.

Any help much appreciated.
Thanks
BN
 
First one will cause a compile time error because the number is too big.

Second one may cause a run time error because the result, after multiplication, is too big. You may get an overflow exception c0000095
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top