IBITS(3) - [BIT:COPY] Extraction of a subset of bits
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
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
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.
POS + LEN shall be less than or equal to BIT_SIZE(I).
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.
The return value is composed of the selected bits right-justified, left-padded with zeros.
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(b0101010101011101,kind=int16) write(*,(b16.16,1x,i0)) ibits(i,3,3), ibits(i,3,3)Results:! we can illustrate this as ! #-- position 15 ! | #-- position 0 ! | <-- +len | ! V V ! 5432109876543210 i =int(b1111111111111111,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(b0010011000000000,kind=int16) j=ibits(i,9,5) write(*,(a,b16.16))j=,j end program demo_ibits
> 7 > 7 > 0000000000000011 3 > j=0000000000001111 > j=0000000000010011
Fortran 95
BTEST(3), IAND(3), IBCLR(3), IBSET(3), IEOR(3), IOR(3), MVBITS(3), NOT(3)
Fortran intrinsic descriptions (license: MIT) @urbanjost
Nemo Release 3.1 | ibits (3fortran) | November 02, 2024 |