Two's complement on an unsigned integer in VB.NET
How can I implement a two's complement in VB.NET using unsigned integer types such as Byte, UShort, UInteger and UL开发者_如何学Goong? Can I cast a UInteger to an Integer?
No, you can't cast. That will result in an overflow exception for large values.
You can, however, do this:
intValue = BitConverter.ToInt32(BitConverter.GetBytes(uintValue), 0)
But what stops you from doing the math with the unsigned values without casting them to something? It just works.
精彩评论