1

Hi i want to calculate

2^(256bit number)

in java, but biginteger's pow function just can handle ints.

How can i calculate with larger numbers?

Is there any library?

i want to calculate all numbers from

2^0
2^1
2^2
...
2^(10^77)
wutzebaer
  • 14,365
  • 19
  • 99
  • 170
  • You can always use more pows... (if not for the bit shift). Curious however: Why? – ppeterka Sep 08 '13 at 20:19
  • possible duplicate of [Very Large Numbers in Java Without using java.math.BigInteger](http://stackoverflow.com/questions/5318068/very-large-numbers-in-java-without-using-java-math-biginteger) – leonm Sep 08 '13 at 20:20
  • For this particular case you can use shiftLeft, but it is a weird oversight. – Antimony Sep 08 '13 at 20:20
  • Just write a 1 and put `number` times 0 to get the binary representation, it can't be hard to get this into whatever other form you need? – mvds Sep 08 '13 at 20:20

1 Answers1

4

I suspect the reason they didn't bother including anything like this is that in most cases, the number would be too big to represent.

Consider 2^(256 bit number). The result has (256bit number) bits, meaning that it takes more memory then there are particles in the universe.

So you'll have to find a different way to represent your logic. Perhaps you could do it symbolically.

It would be possible to do 2^(2^32) and exponents close to that, but this was probably seen as a niche case that they just didn't bother adding a function for.

Antimony
  • 37,781
  • 10
  • 100
  • 107
  • I've been wondering also... According to [this](http://www.universetoday.com/36302/atoms-in-the-universe/#ixzz2eKpF46oC): "The number of atoms in the entire observable universe is estimated to be within the range of 10^78 to 10^82." – ppeterka Sep 08 '13 at 20:27
  • 2^256 just happens to be 10^77, so I was actually closer then I thought. Anyway, it's just a rough comparison. The point is that it's bigger then you could ever reasonably compute. – Antimony Sep 08 '13 at 20:29
  • 1
    I didn't write to disagree, or pick nits, I'd totally consider numbers of this magnitue useless for just about anything... Frankly, I can not think of any practical use... – ppeterka Sep 08 '13 at 20:30
  • There isn't a practical use for numbers above 10^100 IMHO. And in the real world you can't measure many constants better than to 13 digits. So really a `double` is all you need in the real world. ;) – Peter Lawrey Sep 08 '13 at 20:36
  • why whould the representation of this number so much memory? It is a 10 with 77 zeros.... a usecase are cryptographic keys – wutzebaer Sep 08 '13 at 20:41
  • Most modern cryptographic keys have O(10^3) bits, not O(10^77) bits. 10 with 77 zeroes is the number of _digits_ you're storing, not the _value_ you're storing. You can totally store numbers like 10^77, but 2^(10^77) is impractical. – Louis Wasserman Sep 08 '13 at 20:51
  • 1
    @wutzebaer 256 is not a 256 bit number, it is a 8 bit number. We are speaking of numbers that have 2^256 or 10^77 as *exponent*. – Ingo Sep 08 '13 at 20:51
  • but for Diffie-Hellman i have to calculate 5^(256bitnumber)mod(256bitnumber) or is there a mathematical trick? – wutzebaer Sep 08 '13 at 21:04
  • @wutz If you're doing it with a modulus it's easy. And in fact BigInteger has a method for that already. – Antimony Sep 08 '13 at 23:44
  • @Anti whats the name of the function? – wutzebaer Sep 09 '13 at 07:27
  • @Wutz IT's called modPow but you could have just looked that up yourself. – Antimony Sep 09 '13 at 12:33