Why is this returning 0? It should output 18446744073709551615, which should fit

252 Views Asked by At

This code returns 0, where it should return 18446744073709551615, which should fit in a ulong? When I set it to iterate 63 (rather than 64) times I get 9223372036854775808, which is correct.

public static ulong Total()
{
    ulong output = 1ul ; 
    for(var x = 0; x < 64; x++)
    {
        output *= 2;
    }
    return output;
}
2

There are 2 best solutions below

2
On

You are calculating 2^64, which is not 18446744073709551615 but 18446744073709551616. You may notice that when you changed 64 to 63 you got 9223372036854775808 and not 9223372036854775807.

0
On

The calculation you make is out of the capacity of the number of bytes of a ulong needed to contains a big value...

https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/integral-numeric-types

You need to use decimal for example:

public static decimal Total()
{
  decimal output = 1ul;
  for ( var x = 0; x < 64; x++ )
  {
    output *= 2;
  }
  return output;
}

This output 18446744073709551616.

Indeed, if we add the checked keyword to the method using ulong:

public static ulong Total()
{
  ulong output = 1ul;
  for ( var x = 0; x < 64; x++ )
  {
    output = checked(output * 2);
  }
  return output;
}

We get an overflow exception.

You can also use System.Numerics.BigInteger for very huge integers.

https://learn.microsoft.com/dotnet/api/system.numerics.biginteger