trailz(3f) - [FORTRAN:INTRINSIC:BIT INQUIRY] Number of trailing zero bits of an integer
result = trailz(i) integer :: result integer(kind=NNN),intent(in) :: i
TRAILZ returns the number of trailing zero bits of an INTEGER value
I Shall be of type INTEGER.
The type of the return value is the default INTEGER. If all the bits of I are zero, the result value is bit_size(I).
Sample program:
program demo_trailz use, intrinsic :: iso_fortran_env, only : integer_kinds, & & int8, int16, int32, int64 implicit none integer(kind=int64) :: i, value write(*,*)Default integer: write(*,*)bit_size=,bit_size(0) write(*,(1x,i3,1x,i3,1x,b0))-1,trailz(1),-1 write(*,(1x,i3,1x,i3,1x,b0))0,trailz(0),0 write(*,(1x,i3,1x,i3,1x,b0))1,trailz(1),1 write(*,(" huge(0)=",i0,1x,i0,1x,b0))huge(0),trailz(huge(0)),huge(0) write(*,*) write(*,*)integer(kind=int64): do i=-1,62,5 value=2**i write(*,(i19,1x,i3,1x,b64.64))value,trailz(value),value enddo value=huge(i) write(*,(i19,1x,i3,1x,b64.64,"(huge(0_int64))")) & & value,trailz(value),value end program demo_trailzResults:
Default integer: bit_size= 32 -1 0 11111111111111111111111111111111 0 32 0 1 0 1 huge(0)=2147483647 0 1111111111111111111111111111111integer(kind=int64): 0 64 \ 00000000000000000000000000000000000000000000000000000000000000 16 4 \ 00000000000000000000000000000000000000000000000000000000010000 512 9 \ 00000000000000000000000000000000000000000000000000001000000000 16384 14 \ 00000000000000000000000000000000000000000000000100000000000000 524288 19 \ 00000000000000000000000000000000000000000010000000000000000000 16777216 24 \ 00000000000000000000000000000000000001000000000000000000000000 536870912 29 \ 00000000000000000000000000000000100000000000000000000000000000 17179869184 34 \ 00000000000000000000000000010000000000000000000000000000000000 549755813888 39 \ 00000000000000000000001000000000000000000000000000000000000000 17592186044416 44 \ 00000000000000000100000000000000000000000000000000000000000000 562949953421312 49 \ 00000000000010000000000000000000000000000000000000000000000000 18014398509481984 54 \ 00000001000000000000000000000000000000000000000000000000000000 576460752303423488 59 \ 00100000000000000000000000000000000000000000000000000000000000 9223372036854775807 0 \ 0111111111111111111111111111111111111111111111111111111111111111\ (huge(0_int64))
[[Fortran 2008]] and later
[[Elemental function]]
bit_size(3), popcnt(3), poppar(3), leadz(3)
trailz (3) | March 11, 2021 |