D.2.2 Bit Function Arguments

The intrinsic functions IAND, IOR, IEOR, and NOT operate on all of the bits of their argument or arguments. Bit 0 of the result is the result of applying the specified logical operation to bit 0 of the argument or arguments. Bit 1 of the result is the result of applying the specified logical operation to bit 1 of the argument or arguments, and so on for all of the bits of the result.

The functions ISHFT and ISHFTC shift binary patterns. A positive shift count indicates a left shift, while a negative shift count indicates a right shift. ISHFT specifies a logical shift; bits shifted out of one end are lost and zeros are shifted in at the other end. ISHFTC performs a circular shift; bits shifted out at one end are shifted back in at the other end.

The functions IBSET, IBCLR, BTEST, and IBITS and the subroutine MVBITS operate on bit fields.

A bit field is a contiguous group of bits within a binary pattern. Bit fields are specified by a starting bit position and a length. A bit field must be entirely contained in its source operand.

For example, the integer 47 is represented by the following:

Binary pattern:  0...0101111 
Bit position:  n...6543210 
  Where n is the number of bit positions in the numeric storage unit. 

You can refer to the bit field contained in bits 3 through 6 by specifying a starting position of 3 and a length of 4.

Negative integers are represented in twos complement notation. For example, the integer -47 is represented by the following:

Binary pattern:  1...1010001 
Bit position:  n...6543210 
  Where n is the number of bit positions in the numeric storage unit

The value of bit position n is as follows:

1 for a negative number
0 for a non-negative number

All the high-order bits in the pattern from the last significant bit of the value up to bit n are the same as bit n.

IBITS and MVBITS operate on general bit fields. Both the starting position of a bit field and its length are arguments to these intrinsics. IBSET, IBCLR, and BTEST operate on 1-bit fields. They do not require a length argument.

For IBSET, IBCLR, and BTEST, the bit position range is as follows:

For IBITS, the bit position can be any number. The length range is 0 to 31 on VAX processors, and 0 to 63 on Alpha processors.

The following example shows IBSET, IBCLR, and BTEST:

I = 4
J = IBSET (I,5)
TYPE *, 'J = ',J
K = IBCLR (J,2)
TYPE *, 'K = ',K
TYPE *, 'Bit 2 of K is ',BTEST(K,2)
END

The results are: J = 36, K = 32, and Bit 2 of K is F.

For optimum selection of performance and memory requirements, Compaq Fortran 77 provides the following integer data types:

Data Type  Storage Required (in bytes) 
INTEGER*2  2 
INTEGER*4  4 
INTEGER*8[1]  8 

[1] Alpha only

The bit manipulation functions each have a generic form that operates on all of these integer types and a specific form for each type.

When you specify the intrinsic functions that refer to bit positions or that shift binary patterns within a storage unit, be careful that you do not create a value that is outside the range of integers representable by the data type. If you shift by an amount greater than or equal to the size of the object you're shifting, the result is 0.

Consider the following:

INTEGER*2 I,J
I = 1
J = 17
I = ISHFT(I,J)

The variables I and J have INTEGER*2 data type. Therefore, the generic function ISHFT maps to the specific function IISHFT, which returns an INTEGER*2 result. INTEGER*2 results must be in the range -32768 to 32767, but the value 1, shifted left 17 positions, yields the binary pattern 1 followed by 17 zeros, which represents the integer 131072. In this case, the result in I is 0.

The previous example would be valid if I was INTEGER*4, because ISHFT would then map to the specific function JISHFT, which returns an INTEGER*4 value.

If ISHFT is called with a constant first argument, the result will either be the default integer size or the smallest integer size that can contain the first argument, whichever is larger.

For More Information:


Previous Page Next Page Table of Contents