Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# date types convertion same as c++

How do I convert ulong to long with same result as I get in c++.

C++

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

C#

ulong ul = 3633091313;
long l = (long)ul;
l is 3633091313
like image 570
Teamol Avatar asked Jan 28 '23 17:01

Teamol


1 Answers

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

In C# that corresponds to converting to int:

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

Demo.

like image 194
Sergey Kalinichenko Avatar answered Jan 31 '23 08:01

Sergey Kalinichenko