Posts

Showing posts from April, 2019

Unwrapped overflowed integer values in C# where large negative integers overflow and appear as negative values.

You may find that when working with large integer values that these numbers suddenly become negative numbers. This can be seen in the PowerShell registry editor and also in some SNMP MIBs. The reason why this happens is simple. The last bit of a signed integer determines whether the number is negative or positive, whereas on an unsigned integer this bit is part of the number and therefore the number can be twice as big. UInt32.MaxValue = 4294967295 1 1111111111111111111111111111111 Int32.MaxValue = 2147483647 0 1111111111111111111111111111111 The problem of wrapping occurs when you exceed 2147483647 - lets say 214748364 8 which converts in bits as the following 10000000000000000000000000000000 This works fine in a UInt32, however if you just take these bits and stick them in an Int32 the bits fit (there are still 32 bits available) however the value returns as -2147483648 because the negative values are working backwards this is the "highest" negative value