I needed to find the maximum possible value for an unsigned 64-bit int, what better way that to write a script a groovy to find it, and numbers of bits if I need it again? One problem is that java cannot handle unsigned values so the only option is to use the next biggest primitive but a long is the biggest int type available, except for the BigInteger which can handle any value. You can force a number in groovy to be a BigInteger by suffixing the number that is being assigned with G.
def bits = 64;
def maxValue = 0G;
def bitValue = 1G;
for (int i=0; i<bits; i++)
{
maxValue += bitValue
bitValue += bitValue
}
println maxValue
------------------------------
18446744073709551615
No comments:
Post a Comment