Manual Reference Pages  - trailz (3fortran)

NAME

TRAILZ(3) - [BIT:COUNT] Number of trailing zero bits of an integer

SYNOPSIS

result = trailz(i)

      elemental integer function trailz(i)

integer(kind=**),intent(in) :: i

CHARACTERISTICS

o I is an integer of any kind.
o the result is an integer of default kind

DESCRIPTION

TRAILZ(3) returns the number of trailing zero bits of an integer value.

OPTIONS

o I : the value to count trailing zero bits in

RESULT

The number of trailing rightmost zero bits in an integer value after the last non-zero bit.

           >      right-most non-zero bit
           >                 V
           >  |0|0|0|1|1|1|0|1|0|0|0|0|0|0|
           >  ^               |___________| trailing zero bits
           >   bit_size(i)

If all the bits of I are zero, the result is the size of the input value in bits, ie. BIT_SIZE(I).

The result may also be seen as the position of the rightmost 1 bit in I, starting with the rightmost bit being zero and counting to the left.

EXAMPLES

Sample program:

    program demo_trailz

! some common integer kinds use, intrinsic :: iso_fortran_env, only : & & integer_kinds, int8, int16, int32, int64

implicit none

! a handy format character(len=*),parameter :: & & show = ’(1x,"value=",i4,", value(bits)=",b32.32,1x,", trailz=",i3)’

integer(kind=int64) :: bigi ! basics write(*,*)’Note default integer is’,bit_size(0),’bits’ print show, -1, -1, trailz(-1) print show, 0, 0, trailz(0) print show, 1, 1, trailz(1) print show, 96, 96, trailz(96) ! elemental print *, ’elemental and any integer kind:’ bigi=2**5 write(*,*) trailz( [ bigi, bigi*256, bigi/2 ] ) write(*,’(1x,b64.64)’)[ bigi, bigi*256, bigi/2 ]

end program demo_trailz

Results:

     >  Note default integer is          32 bits
     >  value=  -1, value(bits)=11111111111111111111111111111111 , trailz=  0
     >  value=   0, value(bits)=00000000000000000000000000000000 , trailz= 32
     >  value=   1, value(bits)=00000000000000000000000000000001 , trailz=  0
     >  value=  96, value(bits)=00000000000000000000000001100000 , trailz=  5
     >  elemental and any integer kind:
     >            5          13           4
     >  0000000000000000000000000000000000000000000000000000000000100000
     >  0000000000000000000000000000000000000000000000000010000000000000
     >  0000000000000000000000000000000000000000000000000000000000010000

STANDARD

Fortran 2008

SEE ALSO

BIT_SIZE(3), POPCNT(3), POPPAR(3), LEADZ(3)

Fortran intrinsic descriptions (license: MIT) @urbanjost


Nemo Release 3.1 trailz (3fortran) November 02, 2024
Generated by manServer 1.08 from e31b9ce1-34fc-4dbe-bd0c-c5329c55921d using man macros.