Fortran Library Reference

bit: Bit Functions: and, or, ..., bit, setbit, ...

The definitions are:

and( word1, word2 )

Computes bitwise and of its arguments.

or( word1, word2 )

Computes bitwise inclusive or of its arguments.

xor( word1, word2 )

Computes bitwise exclusive or of its arguments.

not( word )

Returns bitwise complement of its argument.

lshift( word, nbits )

Logical left shift with no end around carry. 

rshift( word, nbits )

Arithmetic right shift with sign extension. 

call bis( bitnum, word )

Sets bit bitnum in word to 1.

call bic( bitnum, word )

Clears bit bitnum in word to 0.

bit( bitnum, word )

Tests bit bitnum in word and returns .true. if the bit is 1, .false. if it is 0.

call setbit(bitnum,word,state)

Sets bit bitnum in word to 1 if state is nonzero, and clears it otherwise.

The alternate external versions for MIL-STD-1753 are:

iand( m, n )

Computes the bitwise and of its arguments.

ior( m, n )

Computes the bitwise inclusive or of its arguments.

ieor( m, n )

Computes the bitwise exclusive or of its arguments.

ishft( m, k )

Is a logical shift with no end around carry (left if k>0, right if k<0).

ishftc( m, k, ic )

Circular shift: right-most ic bits of m are left-shifted circularly k places.

ibits( m, i, len )

Extracts bits: from m, starting at bit i, extracts len bits.

ibset( m, i )

Sets bit: return value is equal to word m with bit number i set to 1.

ibclr( m, i )

Clears bit: return value is equal to word m with bit number i set to 0.

btest( m, i )

Tests bit i in m; returns .true. if the bit is 1, and .false. if it is 0.

See also "mvbits: Move a Bit Field ", and the chapter on Intrinsic Functions in the FORTRAN 77 Reference Manual.

Usage: and, or, xor, not, rshift, lshift

For the intrinsic functions:

x = and( word1, word2 )

x = or( word1, word2 )

x = xor( word1, word2 )

x = not( word )

x = rshift( word, nbits )

x = lshift( word, nbits )

word, word1, word2, nbits are integer input arguments. These are intrinsic functions expanded inline by the compiler. The data type returned is that of the first argument.

No test is made for a reasonable value of nbits.

Example: and, or, xor, not:


demo% cat tandornot.f
    print 1, and(7,4), or(7,4), xor(7,4), not(4)
 1    format(4x 'and(7,4)', 5x 'or(7,4)', 4x 'xor(7,4)',
&          6x 'not(4)'/4o12.11)
    end
demo% f77 -silent tandornot.f
demo% a.out
    and(7,4)     or(7,4)    xor(7,4)      not(4)
 00000000004 00000000007 00000000003 37777777773
demo%

Example: lshift, rshift:


    integer*4 lshift, rshift
    print 1, lshift(7,1), rshift(4,1)
 1    format(1x 'lshift(7,1)', 1x 'rshift(4,1)'/2o12.11)
    end
demo% f77 -silent tlrshift.f
demo% a.out
 lshift(7,1) rshift(4,1)
 00000000016 00000000002
demo%

Usage: bic, bis, bit, setbit

call bic( bitnum, word )

call bis( bitnum, word )

call setbit( bitnum, word, state )

LOGICAL bit x = bit( bitnum, word )

bitnum, state, and word are INTEGER*4 input arguments. Function bit() returns a logical value.

Bits are numbered so that bit 0 is the least significant bit, and bit 31 is the most significant.

bic, bis, and setbit are external subroutines. bit is an external function.

Example 3: bic, bis, setbit, bit:


    integer*4 bitnum/2/, state/0/, word/7/
    logical bit
    print 1, word
 1    format(13x 'word', o12.11)
    call bic( bitnum, word )
    print 2, word
 2    format('after bic(2,word)', o12.11)
    call bis( bitnum, word )
    print 3, word
 3    format('after bis(2,word)', o12.11)
    call setbit( bitnum, word, state )
    print 4, word
 4    format('after setbit(2,word,0)', o12.11)
    print 5, bit(bitnum, word)
 5    format('bit(2,word)', L )
    end
<output>
             word 00000000007
after bic(2,word) 00000000003
after bis(2,word) 00000000007
after setbit(2,word,0) 00000000003
bit(2,word) F