Unsigned integers (The GNU Fortran Compiler) (original) (raw)
5.3.1 Unsigned integers ¶
If the -funsigned option is given, GNU Fortran supports unsigned integers according toJ3/24-116. The data type is called UNSIGNED
. For an unsigned type with n
bits, it implements integer arithmetic modulo 2**n
, comparable to the unsigned
data type in C.
The data type has KIND
numbers comparable to other Fortran data types, which can be selected via the SELECTED_UNSIGNED_KIND
function.
Mixed arithmetic, comparisons and assignment between UNSIGNED
and 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’, ‘O’ and ‘Z’ descriptors, plus unformatted I/O.
Here is a small, somewhat contrived example of their use:
program main use iso_fortran_env, only : uint64 unsigned(kind=uint64) :: v v = huge(v) - 32u_uint64 print *,v end program main
which outputs the number 18446744073709551583.
Arithmetic operations work on unsigned integers, also for exponentiation. As an extension to J3/24-116.txt, unary minus and exponentiation of unsigned integers are permitted unless-pedantic
is in force.
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 that would overflow are rejected with-pedantic
.
SELECT CASE
is supported for unsigned integers.
The following intrinsics take unsigned arguments:
BGE
, see BGE — Bitwise greater than or equal toBGT
, see BGT — Bitwise greater thanBIT_SIZE
, see BIT_SIZE — Bit size inquiry functionBLE
, see BLE — Bitwise less than or equal toBLT
, see BLT — Bitwise less thanCMPLX
, see CMPLX — Complex conversion functionCSHIFT
, see CSHIFT — Circular shift elements of an arrayDIGITS
, see DIGITS — Significant binary digits functionDOT_PRODUCT
, see DOT_PRODUCT — Dot product functionDSHIFTL
, see DSHIFTL — Combined left shiftDSHIFTR
, see DSHIFTR — Combined right shiftEOSHIFT
, see EOSHIFT — End-off shift elements of an arrayFINDLOC
, see FINDLOC — Search an array for a valueHUGE
, see HUGE — Largest number of a kindIALL
, see IALL — Bitwise AND of array elementsIAND
, see IAND — Bitwise logical andIANY
, see IANY — Bitwise OR of array elementsIBCLR
, see IBCLR — Clear bitIBITS
, see IBITS — Bit extractionIBSET
, see IBSET — Set bitIEOR
, see IEOR — Bitwise logical exclusive orINT
, see INT — Convert to integer typeIOR
, see IOR — Bitwise logical orIPARITY
, see IPARITY — Bitwise XOR of array elementsISHFT
, see ISHFT — Shift bitsISHFTC
, see ISHFTC — Shift bits circularlyMATMUL
, see MATMUL — matrix multiplicationMAX
, see MAX — Maximum value of an argument listMAXLOC
, see MAXLOC — Location of the maximum value within an arrayMAXVAL
, see MAXVAL — Maximum value of an arrayMERGE
, see MERGE — Merge variablesMERGE_BITS
, see MERGE_BITS — Merge of bits under maskMIN
, see MIN — Minimum value of an argument listMINLOC
, see MINLOC — Location of the minimum value within an arrayMINVAL
, see MINVAL — Minimum value of an arrayMOD
, see MOD — Remainder functionMODULO
, see MODULO — Modulo functionMVBITS
, see MVBITS — Move bits from one integer to anotherNOT
, see NOT — Logical negationOUT_OF_RANGE
, see OUT_OF_RANGE — Range check for numerical conversionPRODUCT
, see PRODUCT — Product of array elementsRANDOM_NUMBER
, see RANDOM_NUMBER — Pseudo-random numberRANGE
, see RANGE — Decimal exponent rangeREAL
, see REAL — Convert to real typeSHIFTA
, see SHIFTA — Right shift with fillSHIFTL
, see SHIFTL — Left shiftSHIFTR
, see SHIFTR — Right shiftSUM
, see SUM — Sum of array elementsTRANSPOSE
, see TRANSPOSE — Transpose an array of rank twoTRANSFER
, see TRANSFER — Transfer bit patterns
The following intrinsics are enabled with -funsigned:
UINT
, see UINT – Convert to UNSIGNED typeUMASKL
, see UMASKL — Unsigned left justified maskUMASKR
, see UMASKR — Unsigned right justified maskSELECTED_UNSIGNED_KIND
, see SELECTED_UNSIGNED_KIND — Choose unsigned kind
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
.