RosettaCodeData/Task/Integer-overflow/FreeBASIC/integer-overflow.freebasic

51 lines
1.8 KiB
Plaintext

#include <stdio.h>
<lang freebasic>' FB 1.05.0 Win64
' The suffixes L, LL, UL and ULL are added to the numbers to make it
' clear to the compiler that they are to be treated as:
' signed 4 byte, signed 8 byte, unsigned 4 byte and unsigned 8 byte
' integers, respectively.
' Integer types in FB are freely convertible to each other.
' In general if the result of a computation would otherwise overflow
' it is converted to a higher integer type.
' Consequently, although the calculations are the same as the C example,
' the results for the 32-bit integers are arithmetically correct (and different
' therefore from the C results) because they are converted to 8 byte integers.
' However, as 8 byte integers are the largest integral type, no higher conversions are
' possible and so the results 'wrap round'. The 64-bit results are therefore the
' same as the C examples except the one where the compiler warns that there is an overflow
' which, frankly, I don't understand.
Print "Signed 32-bit:"
Print -(-2147483647L-1L)
Print 2000000000L + 2000000000L
Print -2147483647L - 2147483647L
Print 46341L * 46341L
Print (-2147483647L-1L) \ -1L
Print
Print "Signed 64-bit:"
Print -(-9223372036854775807LL-1LL)
Print 5000000000000000000LL + 5000000000000000000LL
Print -9223372036854775807LL - 9223372036854775807LL
Print 3037000500LL * 3037000500LL
Print (-9223372036854775807LL - 1LL) \ -1LL ' compiler warning : Overflow in constant conversion
Print
Print "Unsigned 32-bit:"
Print -4294967295UL
Print 3000000000UL + 3000000000UL
Print 2147483647UL - 4294967295UL
Print 65537UL * 65537UL
Print
Print "Unsigned 64-bit:"
Print -18446744073709551615ULL ' compiler warning : Implicit conversion
Print 10000000000000000000ULL + 10000000000000000000ULL
Print 9223372036854775807ULL - 18446744073709551615ULL
Print 4294967296ULL * 4294967296ULL
Print
Print "Press any key to quit"
Sleep