Is there a safe way to parse Uint numbers into positive Long numbers and have a one to one matching between that uint and the long number??
Asked
Active
Viewed 52 times
0
-
if I receive a uint number and just cast it as (long)number, the result can also be negative, because in the long representation that sign bit is treated as part of the value – Sep 18 '16 at 16:23
-
2The sign is contained only in the MSB (highest bit). A 32 bit UINT (0 to ~4bn) is wholly represented in a 64 bit long, with 31 bits to spare. – StuartLC Sep 18 '16 at 16:41
1 Answers
1
uint i = 0;
long o = Convert.ToInt64(i);
or
uint i = 0;
long o = (long)i;

rlee
- 293
- 1
- 8
-
by doing Convert.ToInt64(i) the result could also be negative as a long, or can't it? Is there a difference between this way and just doing (long)i? – Sep 18 '16 at 16:27
-
if you have an uint as input, you can't possibly get a negative int64, so just casting to long is good enough – rlee Sep 18 '16 at 16:31
-
1011 (binary) as unsigned value is +11. convert this number to signed and the value you will get is -3. Isn't that correct? – Sep 18 '16 at 16:34
-