I am stuck with an exercise at JetBrain Academy, with BigInteger(Kotlin). I should write a code snippet to convert a (BigInteger) number of exbibyte into bits: if the input is 1 as BigInteger, it displays 9223372036854775808. In the hints it says I should use pow(63).(Exbibyte = 2^63 = 9 223 372 036 854 775 808 Bit) My problem is if I use pow(), I can use only double as the biggest range. But with double I cannot display a BigInteger number.
Asked
Active
Viewed 86 times
0
-
Using `BigIntegers` like this may not be a good idea. However, you can take a look at this question about Java that seems related: [BigInteger.pow(BigInteger)?](https://stackoverflow.com/questions/4582277/biginteger-powbiginteger) and its answers. In particular, [this answer](https://stackoverflow.com/a/72335324/11811412) seems to be what you are looking for. Anyway, try to also read all other answers to better understand why it's there's no default way of doing so. – Nicholas Obert Jul 22 '22 at 05:21
-
Multiplying by an integral power of 2 is equivalent to shifting left. So to multiply X by 2^63 you can shift X left by 63. – President James K. Polk Jul 22 '22 at 11:58
1 Answers
1
In your case, I hope it should work:
val exbibytes = BigInteger.valueOf(1)
val oneExbibyteInBits = BigInteger.valueOf(2).pow(63)
val exbibytesInBits = exbibytes.multiply(oneExbibyteInBits)

plplmax
- 1,765
- 8
- 19