02.07.2013 Views

HP Fortran Programmer's Reference

HP Fortran Programmer's Reference

HP Fortran Programmer's Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Expressions and assignment<br />

Expressions<br />

j = 3<br />

PRINT *, i .AND. j<br />

! Output from the PRINT statement: 1<br />

The next example shows the use of logical operators to perform bit-masking operations.<br />

INTEGER(2) mask2<br />

INTEGER(4) mask4<br />

DATA mask2/ -4 /<br />

DATA mask4/Z"ccc2"/<br />

mask4 = mask4 .NEQV. mask2 !set mask4 to Z"ffff333e"<br />

mask2 = .NOT. mask4 !set mask2 to Z"ccc1"<br />

The next example makes a standard-conforming reference to a bit manipulation intrinsic:<br />

INTEGER :: mask = 65535<br />

LOGICAL :: is_even = .FALSE.<br />

IF (IAND(mask,1) /= 0) is_even = .TRUE.<br />

<strong>HP</strong> <strong>Fortran</strong> allows the following nonstandard version of the preceding example:<br />

LOGICAL :: mask = z"ffff"<br />

INTEGER :: is_even = .FALSE.<br />

IF (IAND(mask,1)) is_even = .TRUE.<br />

Operator precedence<br />

When an expression expands to<br />

operand1 operator1 operand2 operator2 operand3 ...<br />

each operator is assigned a precedence. The defined order of evaluation is that any<br />

subexpressions containing an operator with higher precedence than the adjacent operators is<br />

evaluated first. Where operators are of equal precedence, evaluation is from left to right. The<br />

exception to this rule is the exponentiation operator (**), which is evaluated from right to left.<br />

Any expression or subexpression may be enclosed in parentheses. These expressions are<br />

always evaluated first, using the rules explained above. This usage of parentheses is therefore<br />

equivalent to normal mathematical usage.<br />

Table 5-2 lists the precedence of the operators, and Table 5-3 gives example expressions that<br />

illustrate operator precedence.<br />

Table 5-2 Operator precedence<br />

Precedence Operators<br />

88<br />

Chapter 5

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!