Unsigned integers (The GNU Fortran Compiler) (original) (raw)


5.3.1 Unsigned integers

GNU Fortran supports unsigned integers according toJ3/24-116. The data type is called UNSIGNED. For an unsigned type with nnn bits, it implements integer arithmetic modulo 2**n, comparable to theunsigned data type in C.

The data type has KIND numbers comparable to other Fortran data types, which can be selected via the SELECTED_UNSIGNED_KINDfunction.

Mixed arithmetic, comparisons and assignment between UNSIGNEDand other types are only possible via explicit conversion. Conversion from UNSIGNED to other types is done via type conversion functions like INT or REAL. Conversion from other types to UNSIGNED is done via UINT. Unsigned variables cannot be used as index variables in DO loops or as array indices.

Unsigned numbers have a trailing u as suffix, optionally followed by a KIND number separated by an underscore.

Input and output can be done using the I, B, Oand Z descriptors, plus unformatted I/O.

Here is a small, somewhat contrived example of their use:

program main unsigned(kind=8) :: v v = huge(v) - 32u_8 print *,v end program main

which will output the number 18446744073709551583.

Arithmetic operations work on unsigned integers, except for exponentiation, which is prohibited. Unary minus is not permitted when -pedanticis in force; this prohibition is part of J3/24-116.txt.

In intrinsic procedures, unsigned arguments are typically permitted for arguments for the data to be processed, analogous to the use of REAL arguments. Unsigned values are prohibited as index variables in DO loops and as array indices.

Unsigned numbers can be read and written using list-directed, formatted and unformatted I/O. For formatted I/O, the B,I, O and Z descriptors are valid. Negative values and values which would overflow are rejected with-pedantic.

As of now, the following intrinsics take unsigned arguments:

The following constants have been added to the intrinsicISO_C_BINDING module: c_unsigned,c_unsigned_short, c_unsigned_char,c_unsigned_long, c_unsigned_long_long,c_uintmax_t, c_uint8_t, c_uint16_t,c_uint32_t, c_uint64_t, c_uint128_t,c_uint_fast8_t, c_uint_fast16_t, c_uint_fast32_t,c_uint_fast64_t, c_uint_fast128_t,c_uint_least8_t, c_uint_least16_t, c_uint_least32_t,c_uint_least64_t and c_uint_least128_t.

The following constants have been added to the intrinsicISO_FORTRAN_ENV module: uint8, uint16,uint32 and uint64.