How to define a terabyte constant in Kotlin

1 Answer

0 votes
/*
    Kotlin's Long type is a signed 64‑bit integer.
    Maximum value: 9_223_372_036_854_775_807  (~9.22 × 10^18)

    Terabyte sizes:

        1 TiB = 2^40  = 1_099_511_627_776 bytes
        1 TB  = 10^12 = 1_000_000_000_000 bytes

    Both values fit safely inside a Long.
*/

object Terabytes {

    // Binary terabyte (tebibyte), using a bit shift.
    // 1L shl 40 = 2^40 bytes.
    const val TERABYTE_TIB: Long = 1L shl 40

    // Decimal terabyte (SI), using a readable numeric literal.
    const val TERABYTE_TB: Long = 1_000_000_000_000L
}

fun main() {
    println("1 TiB = ${Terabytes.TERABYTE_TIB} bytes")
    println("1 TB  = ${Terabytes.TERABYTE_TB} bytes")
}



/*
run:

1 TiB = 1099511627776 bytes
1 TB  = 1000000000000 bytes

*/

 



answered May 3 by avibootz
...