Manual Reference Pages  - ibits (3fortran)

NAME

IBITS(3) - [BIT:COPY] Extraction of a subset of bits

SYNOPSIS

result = ibits(i, pos, len)

         elemental integer(kind=KIND) function ibits(i,pos,len)

integer(kind=KIND),intent(in) :: i integer(kind=**),intent(in) :: pos integer(kind=**),intent(in) :: len

CHARACTERISTICS

o a kind designated as ** may be any supported integer kind
o I may be any supported integer kind as well
o the return value will be the same kind as I

DESCRIPTION

IBITS(3) extracts a field of bits from I, starting from bit position POS and extending left for a total of LEN bits.

The result is then right-justified and the remaining left-most bits in the result are zeroed.

The position POS is calculated assuming the right-most bit is zero and the positions increment to the left.

OPTIONS

o I : The value to extract bits from
o POS : The position of the bit to start copying at. POS is non-negative.
o LEN : the number of bits to copy from I. It must be non-negative.
POS + LEN shall be less than or equal to BIT_SIZE(I).

RESULT

The return value is composed of the selected bits right-justified, left-padded with zeros.

EXAMPLES

Sample program:

    program demo_ibits
    use,intrinsic :: iso_fortran_env,  only : int8, int16, int32, int64
    implicit none
    integer(kind=int16) :: i,j
      ! basic usage
       print *,ibits (14, 1, 3) ! should be seven
       print *,ibits(-1,10,3)   ! and so is this
       ! it is easier to see using binary representation
       i=int(b’0101010101011101’,kind=int16)
       write(*,’(b16.16,1x,i0)’) ibits(i,3,3), ibits(i,3,3)

! we can illustrate this as ! #-- position 15 ! | #-- position 0 ! | <-- +len | ! V V ! 5432109876543210 i =int(b’1111111111111111’,kind=int16) ! ^^^^ j=ibits(i,10,4) ! start at 10th from left and proceed ! left for a total of 4 characters write(*,’(a,b16.16)’)’j=’,j ! lets do something less ambiguous i =int(b’0010011000000000’,kind=int16) j=ibits(i,9,5) write(*,’(a,b16.16)’)’j=’,j end program demo_ibits

Results:

     > 7
     > 7
     > 0000000000000011 3
     > j=0000000000001111
     > j=0000000000010011

STANDARD

Fortran 95

SEE ALSO

BTEST(3), IAND(3), IBCLR(3), IBSET(3), IEOR(3), IOR(3), MVBITS(3), NOT(3)

fortran-lang intrinsic descriptions (license: MIT) @urbanjost


Nemo Release 3.1 ibits (3fortran) April 28, 2024
Generated by manServer 1.08 from d6f99f56-168c-4605-af08-01d0cf8a1f71 using man macros.