0

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??

  • 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
  • 2
    The 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 Answers1

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
  • see StuartLC comment above – rlee Sep 18 '16 at 16:45