Convert -1 to uint C#

user2089096 picture user2089096 · Mar 17, 2013 · Viewed 10.5k times · Source

Converting -1 to uint will not work "((uint)(-1))" solution?

num10 = ((uint)(-1)) >> (0x20 - num9);

Error: Constant value '-1' cannot be converted to a 'uint' (use 'unchecked' syntax to override)

Answer

p.s.w.g picture p.s.w.g · Mar 17, 2013

Use

uint minus1 = unchecked((uint)-1);
uint num10 = (minus1) >> (0x20 - num9);

Or even better

uint num10 = (uint.MaxValue) >> (0x20u - num9);