Considering the following code snippet:
integer(int8) :: a
integer(int32) :: b
integer(int64) :: c
a = - huge(a) - 1
b = - huge(b) - 1
c = - huge(c) - 1
print *, a, b, c
a = int(b, kind=int8)
c = int(b, kind=int64)
print *, a, b, c
On my system it gives the following output:
-128 -2147483648 -9223372036854775808
0 -2147483648 -2147483648
This suggests that between conversion between integers of different kinds occurs as follows:
- Widening Integer Conversions [less bytes to more bytes] is done via sign-extension.
- Narrowing Integer Conversions [more bytes to less bytes] is done by simply lobbing off the higher-order bits.
This is similar to Java's integer conversion rules.
Is the behavior defined to be standard Fortran? Is there any way to override rule 1 and simply get a normal widening [like Java's Integer.toUnsignedLong()]?
Compiler Information:
GNU Fortran (Rev6, Built by MSYS2 project) 11.2.0
Copyright (C) 2021 Free Software Foundation, Inc.