C # dates conversion types are the same as C ++

How to convert ulong to long with the same result as in C ++.

C ++

unsigned long ul = 3633091313;
long l = (long)ul;
l is -661875983

WITH#

ulong ul = 3633091313;
long l = (long)ul;
l is 3633091313
+4
source share
1 answer

C ++ is longoften a 32-bit data type. It looks like your system does not have enough bits to represent 3633091313, so the result is negative. This behavior is undefined in C ++ ( corresponding to Q & A ).

In C #, which corresponds to converting to int:

UInt64 ul = 3633091313UL;
Int32 l = (int)ul;
Console.WriteLine(l); // prints -661875983

Demo version

+8
source

Source: https://habr.com/ru/post/1693334/


All Articles