开发者

Two's complement on an unsigned integer in VB.NET

开发者 https://www.devze.com 2023-03-15 02:18 出处:网络
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 wil

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消