Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
29
Task/Integer-overflow/Kotlin/integer-overflow.kotlin
Normal file
29
Task/Integer-overflow/Kotlin/integer-overflow.kotlin
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
// The Kotlin compiler can detect expressions of signed constant integers that will overflow.
|
||||
// It cannot detect unsigned integer overflow, however.
|
||||
@Suppress("INTEGER_OVERFLOW")
|
||||
fun main() {
|
||||
println("*** Signed 32 bit integers ***\n")
|
||||
println(-(-2147483647 - 1))
|
||||
println(2000000000 + 2000000000)
|
||||
println(-2147483647 - 2147483647)
|
||||
println(46341 * 46341)
|
||||
println((-2147483647 - 1) / -1)
|
||||
println("\n*** Signed 64 bit integers ***\n")
|
||||
println(-(-9223372036854775807 - 1))
|
||||
println(5000000000000000000 + 5000000000000000000)
|
||||
println(-9223372036854775807 - 9223372036854775807)
|
||||
println(3037000500 * 3037000500)
|
||||
println((-9223372036854775807 - 1) / -1)
|
||||
println("\n*** Unsigned 32 bit integers ***\n")
|
||||
// println(-4294967295U) // this is a compiler error since unsigned integers have no negation operator
|
||||
// println(0U - 4294967295U) // this works
|
||||
println((-4294967295).toUInt()) // converting from the signed Int type also produces the overflow; this is intended behavior of toUInt()
|
||||
println(3000000000U + 3000000000U)
|
||||
println(2147483647U - 4294967295U)
|
||||
println(65537U * 65537U)
|
||||
println("\n*** Unsigned 64 bit integers ***\n")
|
||||
println(0U - 18446744073709551615U) // we cannot convert from a signed type here (since none big enough exists) and have to use subtraction
|
||||
println(10000000000000000000U + 10000000000000000000U)
|
||||
println(9223372036854775807U - 18446744073709551615U)
|
||||
println(4294967296U * 4294967296U)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue