Sun Studio 12: Fortran User's Guide

# 4.5 Unsigned Integers

The Fortran 95 compiler accepts a new data type, UNSIGNED, as an extension to the language. Four KIND parameter values are accepted with UNSIGNED: 1, 2, 4, and 8, corresponding to 1-, 2-, 4-, and 8-byte unsigned integers, respectively.

The form of an unsigned integer constant is a digit-string followed by the upper or lower case letter U, optionally followed by an underscore and kind parameter. The following examples show the maximum values for unsigned integer constants:

 ``` 255u_1 65535u_2 4294967295U_4 18446744073709551615U_8```

Expressed without a kind parameter (12345U), the default is the same as for default integer. This is U_4 but can be changed by the -xtypemap option, which will change the kind type for default unsigned integers.

Declare an unsigned integer variable or array with the UNSIGNED type specifier:

 ``` UNSIGNED U UNSIGNED(KIND=2) :: A UNSIGNED*8 :: B```

## 4.5.1 Arithmetic Expressions

• Binary operations, such as + - * / cannot mix signed and unsigned operands. That is, U*N is illegal if U is declared UNSIGNED, and N is a signed INTEGER.

• Use the UNSIGNED intrinsic function to combine mixed operands in a binary operation, as in U*UNSIGNED(N)

• An exception is when one operand is an unsigned integer and the other is a signed integer constant expression with positive or zero value; the result is an unsigned integer.

• The kind of the result of such a mixed expression is the largest kind of the operands.

Exponentiation of a signed value is signed while exponentiation of an unsigned value is unsigned.

• Unary minus of an unsigned value is unsigned.

• Unsigned operands may mix freely with real, complex operands. (Unsigned operands cannot be mixed with interval operands.)

## 4.5.2 Relational Expressions

Signed and unsigned integer operands may be compared using intrinsic relational operations. The result is based on the unaltered value of the operands.

## 4.5.3 Control Constructs

• The CASE construct accepts unsigned integers as case-expressions.

• Unsigned integers are not permitted as DO loop control variables, or in arithmetic IF control expressions.

## 4.5.4 Input/Output Constructs

• Unsigned integers can be read and written using the I, B, O, and Z edit descriptors.

• They can also be read and written using list-directed and namelist I/O. The written form of an unsigned integer under list-directed or namelist I/O is the same as is used for positive signed integers.

• Unsigned integers can also be read or written using unformatted I/O.

## 4.5.5 Intrinsic Functions

• Unsigned integers are allowed in intrinsic functions, except for SIGN and ABS.

• A new intrinsic function, UNSIGNED, is analogous to INT but produces a result of unsigned type. The form is

UNSIGNED(v [,kind] ).

• Another new intrinsic function, SELECTED_UNSIGNED_KIND( var), returns the kind parameter for var.

• Intrinsic functions do not allow both signed and unsigned integer operands, except for the MAX and MIN functions, which allow both signed and unsigned integer operands only if there is at least one operand of REAL type.

• Unsigned arrays cannot appear as arguments to array intrinsic functions.