5
Answers

unsigned integer help

David Smith

David Smith

11y
1k
1
Below when the number goes negative, I get a large number (4294967290), which is not correct, do anyone know why. I know that Round function returns a double. The large number is 4294967290. What to do see the exact negative number

 test4 = (uint)Math.Round((m_abortTime - DateTime.Now,3).TotalSeconds);
Answers (5)
1
Vulpes

Vulpes

NA 98.3k 1.5m 11y
If you cast to int rather than uint, then this will give you the exact negative number but test4 will need to be an int (or long) rather than a uint variable, otherwise you'll get a conversion error.

As a general rule, it's best to avoid the unsigned integral types unless you really need the additional range or you're inter-operating with unmanaged code which uses unsigned types.
Accepted
0
Jignesh Trivedi

Jignesh Trivedi

NA 61k 14.2m 11y

hi,

As per my knowledge maximum value is supported by uint is 4294967295, when you trying to assign nagative value or cast nagative value then it perform substraction.

Example:

int j = 0;
uint kkk = (uint)(j - 23232);

interanlly it perform 4294967295 + 0 - 23232 + 1 = 4294944064

this only true for nagative value
hope this will help you.

0
Vulpes

Vulpes

NA 98.3k 1.5m 11y
Yes, I'd declare test4 to be either int or, if the result could exceed + or - 2 billion, long.
0
David Smith

David Smith

NA 1.9k 0 11y
Agreed. I was on the slow bus. Can I assigned to a signed type or just declare as a long?
0
Sanjeeb Lenka

Sanjeeb Lenka

NA 22.1k 1.3m 11y
The unsigned versions with the same bit sizes. Effectively, this means they cannot store negative numbers, but on the other hand they can store positive numbers twice as large as their unsigned counterparts.

The limits for int (32 bit) are:

int: –2147483648 to 2147483647
uint: 0 to 4294967295 And for long (64 bit):

long: -9223372036854775808 to 9223372036854775807
ulong: 0 to 18446744073709551615