How to define a petabyte constant in Kotlin

1 Answer

0 votes
/*
    Kotlin's Long type is a signed 64‑bit integer.
    Its maximum value is about 9.22 × 10^18.

    A petabyte fits easily:

        1 PiB = 2^50  = 1,125,899,906,842,624 bytes
        1 PB  = 10^15 = 1,000,000,000,000,000 bytes

    Kotlin allows:
      - bit shifting on Long values
      - readable numeric literals with underscores
      - compile‑time constant evaluation
*/

// Binary petabyte (pebibyte), using a bit shift.
// 1L shl 50 = 2^50 bytes.
const val PETABYTE_PIB: Long = 1L shl 50

// Decimal petabyte (SI petabyte), using a readable numeric literal.
const val PETABYTE_PB: Long = 1_000_000_000_000_000L

fun main() {
    println("1 PiB = $PETABYTE_PIB bytes")
    println("1 PB  = $PETABYTE_PB bytes")
}



/*
run:

1 PiB = 1125899906842624 bytes
1 PB  = 1000000000000000 bytes

*/

 



answered May 3 by avibootz
...