Bits in Fortran

                I have a collection of bits that start with bit m in word 1 and end with bit n in word 2.  I want to write these as a single integer ending with bit n and beginning with bit m

Bits are numbered from right to left, starting with 0

        ¯m                     ¯0

11010101011100001010011111000011

     ¯n                        

10010101011100001010011111000000

The total number of bits in the output word is 32-m+1+n = n+33-m.

Multiply the first word by 2m-1 to form

¯m                     ¯32

01110000101001111100001100000000

Remove a leading 1 from the second word

                The code to do this is in BIF.zip

                Watfor codes in Ansi Fortran.  A non-elegant way doing the operations described below is contained in BITSINFORTRAN.zip  These routines involve reading and writing to a character string in order to use formatting to rearrange the variables.

                The codes in itob.zip use the subroutine itob(ii,iz) where iz is defined by

      DIMENSION IZ(2),II(64)

      REAL*8 TEMP

      EQUIVALENCE (TEMP,IZ(1))

TEMP=1.1D0

IZ =  9999999A 3FF19999

10011001100110011001100110011010

00111111111100011001100110011001

 II 64 TO 32

00111111111100011001100110011001

 I1 AFTER HTOB

 31 TO 0

10011001100110011001100110011010

 63 TO 31

00111111111100011001100110011001

Inline

                The compiler will put arithmetic statement functions inline and can thus speed up the code.  Left and right shifts are coded into INLINE.zip as arithmetic statement functions.  This works with Watcom, but not with Watfor where the external functions do work, but not the arithmetic statement functions (probably because of the integer overflows and also the addition order)  The routines in WFTITOB.zip use external functions for right and left shifts.  These will slow the code down over optimized inline finctions, but this Watfor is not used for speed.

ISHFT

From Msdev Fortran Reference

Purpose

Elemental Intrinsic Function      Logically shifts an integer left or right by a specified number of bits. Zeros are shifted in from the opposite end.

Syntax

result = ISHFT (i, shift)

 

Return Value

Integer. Value of i shifted by shift bits, or an array of such values. If shift is positive, i is shifted left shift bits. If shift is negative, i is shifted right shift bits. Zeros are shifted in from the opposite end.

Remarks

Unlike circular or arithmetic shifts, which can shift ones into the number being shifted, logical shifts shift in zeros only, regardless of the direction or size of the shift. The integer kind, however, still determines the end that bits are shifted out of, which can make a difference in the result. See the example following.

Example

 

INTEGER(1) i, res1

INTEGER(2) j, k(3), res2

i = 10    ! equal to  00001010

j = 10    ! equal to  00000000 00001010

res1  = ISHFT (i, 5)   ! returns 01000000 = 64

res2  = ISHFT (j, 5)   ! returns 00000001 01000000 =

                       ! 320

 

k = ISHFT((/3, 5, 1/), (/1, -1, 0/))   ! returns array

                                       ! /6, 2, 1/


From Watcom (edited by Bob)

Binary Pattern Processing Functions:  Logical Shift

Definition:

                 ishft(j,n) Logical shift

Notes:

 

There are three shift operations:  logical, arithmetic and circular.  These shift operations are implemented as integer functions having two arguments.  The first argument, j, is the value to be shifted and the second argument,  n, is the number of bits to shift.  If n is less than 0, a right shift is performed.  If n is greater than 0, a left shift is performed.  If n is equal to 0, no shift is performed.  Note that the arguments are not modified.

 

In a logical shift, bits shifted out from the left or right are lost.  Zeros are shifted in from the opposite end.

IBITS

Purpose

Elemental Intrinsic Function      Extracts a sequence of bits from a bit pattern.

Syntax

result = IBITS (i, pos, len)

 

i
(Input)  Integer. Can be an array. If an array, must be the same shape as pos and len. Number containing the bit pattern.

pos
(Input)  Integer. Can be an array. If an array, must be the same shape as i and len. Starting position of the sequence to be extracted. The leftmost (most significant) bit of i is in position 1. The argument pos must be positive, and pos + len must be less than or equal to the number of bits in i (to determine the number of bits in i, use BIT_SIZE).

len
(Input)  Integer. Can be an array. If an array, must be the same shape as i and pos. Length of the consecutive bit pattern to be extracted. Must be nonnegative.

Return Value

Integer. Same kind as i. Consecutive sequence of bits extracted from i, or an array of such sequences.

Compatibility

CONSOLE   STANDARD GRAPHICS   QUICKWIN GRAPHICS   WINDOWS   DLL   LIB

Example

 

      I = IBITS(2#1010, 1, 3)  !  returns 2#101 = 5

IBSET

Purpose

Elemental Intrinsic Function      Sets (sets to 1) the specified bit in an integer.

Syntax

result = IBSET (i, pos)

 

i
(Input)  Integer. Can be an array. If an array, must be the same shape as pos. Number containing the bit to be set to 1.

pos
(Input)  Integer. Can be an array. If an array, must be the same shape as i. Position of the bit to be set to 1. The rightmost (least significant) bit of i is in position 0.

Return Value

Integer. Same type as i and equal to i with the bit in position pos set to 1.

Compatibility

CONSOLE   STANDARD GRAPHICS   QUICKWIN GRAPHICS   WINDOWS   DLL   LIB

Example

 

INTEGER I

I = IBSET(8, 2)        ! returns 12 = 1100

IBCLR

                Bits are numbered 31, …, 1,0

Purpose

Elemental Intrinsic Function      Clears (sets to 0) a specified bit in an integer.

Syntax

result = IBCLR (i, pos)

 

i
(Input)  Integer. Can be an array. If an array, must be the same shape as pos. Argument containing the bit to be cleared to 0.

pos
(Input)  Integer. Can be an array. If an array, must be the same shape as i. Position of the bit to be cleared. The rightmost (least significant) bit of i is in position 0.

Return Value

Integer. Same kind as i and equal to i with the bit in position pos set to 0, or an array of such values.

Compatibility

CONSOLE   STANDARD GRAPHICS   QUICKWIN GRAPHICS   WINDOWS   DLL   LIB

Example

 

INTEGER J, K

J = IBCLR(7, 1)      ! returns 5 =  0101

K = IBCLR(5, 1)      ! returns 5 =  0101

Borland C

Bitwise operators 

 

Operators

Use bitwise operators to modify individual bits of a number rather than the whole number.

 

Syntax

 

AND-expression & equality-expression

 

exclusive-OR-expr ^ AND-expression

inclusive-OR-expr exclusive-OR-expression

~expression

shift-expression << additive-expression

 

shift-expression >> additive-expression

 

Operator                What it does

 

&             Bitwise AND: compares two bits and generates a 1 result if both bits are 1; otherwise, it returns 0.

|               Bitwise inclusive OR: compares two bits and generates a 1 result if either or both bits are 1; otherwise, it returns 0.

^              Bitwise exclusive OR: compares two bits and generates a 1 result if the bits are complementary; otherwise, it returns 0.

~              Bitwise complement: inverts each bit. (~ is also used to create destructors.)

>>           Bitwise shift right: moves the bits to the right, discards the far right bit and assigns the leftmost bit   to 0.

 

<<           Bitwise shift left: moves the bits to the left, it discards the far left bit and assigns the rightmost bit to 0.

 

Both operands in a bitwise expression must be of an integral type.

 

Bit value                Results of &, ^, | operations

 

E1            E2            E1 & E2  E1 ^ E2   E1 | E2

 

0              0              0                              0              0

1              0              0                              1              1

0              1              0                              1              1

1              1              1                              0              1