Search code examples
c#mathtypesint32integral

Min and Max value of integral type in C#


Whats the mathematical formulae to calculate the MIN and MAX value of an integral type using your calculator. I know you can use Integer.Max or Integer.Min etc or look it up on msdn however I want to know how to calculate it.


Solution

  • For unsigned types:

    • Min value = 0
    • Max value = (2 ** (number of bits)) - 1

    So, for UInt32:

    Min value = 0
    
    Max value = (2 ** 32) - 1
              = 4294967296 - 1
              = 4294967295
    

    For signed types:

    • Min value = 0 - (2 ** (number of bits - 1))
    • Max value = (2 ** (number of bits - 1)) - 1

    So, for Int32:

    Min value = 0 - (2 ** (32 - 1))
              = 0 - (2 ** 31)
              = 0 - 2147483648
              = -2147483648
    
    Max value = (2 ** (32 - 1)) - 1
              = (2 ** 31) - 1
              = 2147483648 - 1
              = 2147483647