BTEST(3) - [BIT:INQUIRY] Tests a bit of an integer value.
result = btest(i,pos)
elemental logical function btest(i,pos)integer(kind=**),intent(in) :: i integer(kind=**),intent(in) :: pos
o I is an integer of any kind o POS is a integer of any kind o the result is a default logical
BTEST(3) returns logical .true. if the bit at POS in I is set to 1. Position zero is the right-most bit. Bit position increases from right to left up to BITSIZE(I)-1.
o I : The integer containing the bit to be tested o POS : The position of the bit to query. it must be a valid position for the value I; ie. 0 <= POS <= BIT_SIZE(I).
The result is a logical that has the value .true. if bit position POS of I has the value 1 and the value .false. if bit POS of I has the value 0.
Positions of bits in the sequence are numbered from right to left, with the position of the rightmost bit being zero.
Sample program:
program demo_btest implicit none integer :: i, j, pos, a(2,2) logical :: bool character(len=*),parameter :: g=(*(g0))Results:i = 32768 + 1024 + 64 write(*,(a,i0,"=>",b32.32,/))Looking at the integer: ,i
! looking one bit at a time from LOW BIT TO HIGH BIT write(*,g)from bit 0 to bit ,bit_size(i),==> do pos=0,bit_size(i)-1 bool = btest(i, pos) write(*,(l1),advance=no)bool enddo write(*,*)
! a binary format the hard way. ! Note going from bit_size(i) to zero. write(*,*) write(*,g)so for ,i, with a bit size of ,bit_size(i) write(*,(b32.32))i write(*,g)merge(^,_,[(btest(i,j),j=bit_size(i)-1,0,-1)]) write(*,*) write(*,g)and for ,-i, with a bit size of ,bit_size(i) write(*,(b32.32))-i write(*,g)merge(^,_,[(btest(-i,j),j=bit_size(i)-1,0,-1)])
! elemental: ! a(1,:)=[ 1, 2 ] a(2,:)=[ 3, 4 ] write(*,*) write(*,(a,/,*(i2,1x,i2,/)))given the array a ...,a ! the second bit of all the values in a write(*,(a,/,*(l2,1x,l2,/)))the value of btest (a, 2),btest(a,2) ! bits 1,2,3,4 of the value 2 write(*,(a,/,*(l2,1x,l2,/)))the value of btest (2, a),btest(2,a) end program demo_btest
> Looking at the integer: 33856=>11111111111111110111101111000000 > > 00000000000000001000010001000000 > 11111111111111110111101111000000 > 1000010001000000 > 11111111111111110111101111000000 > from bit 0 to bit 32==> > FFFFFFTFFFTFFFFTFFFFFFFFFFFFFFFF > > so for 33856 with a bit size of 32 > 00000000000000001000010001000000 > ________________^____^___^______ > > and for -33856 with a bit size of 32 > 11111111111111110111101111000000 > ^^^^^^^^^^^^^^^^_^^^^_^^^^______ > > given the array a ... > 1 3 > 2 4 > > the value of btest (a, 2) > F F > F T > > the value of btest (2, a) > T F > F F
Fortran 95
IAND(3), IBCLR(3), IBITS(3), IBSET(3), IEOR(3), IOR(3), MVBITS(3), NOT(3)
Fortran intrinsic descriptions (license: MIT) @urbanjost
Nemo Release 3.1 | btest (3fortran) | November 02, 2024 |