LEADZ(3) - [BIT:COUNT] Number of leading zero bits of an integer
result = leadz(i)
elemental integer function leadz(i)integer(kind=**),intent(in) :: i
o I may be an integer of any kind. o the return value is a default integer type.
LEADZ(3) returns the number of leading zero bits of an integer.
o I : integer to count the leading zero bits of.
The number of leading zero bits, taking into account the kind of the input value. If all the bits of I are zero, the result value is BIT_SIZE(I).
The result may also be thought of as BIT_SIZE(I)-1-K where K is the position of the leftmost 1 bit in the input I. Positions are from 0 to bit-size(), with 0 at the right-most bit.
Sample program:
program demo_leadz implicit none integer :: value, i character(len=80) :: fResults:! make a format statement for writing a value as a bit string write(f,("(b",i0,".",i0,")"))bit_size(value),bit_size(value)
! show output for various integer values value=0 do i=-150, 150, 50 value=i write (*,("LEADING ZERO BITS=",i3),advance=no) leadz(value) write (*,(" OF VALUE "),advance=no) write(*,f,advance=no) value write(*,(*(1x,g0))) "AKA",value enddo ! Notes: ! for twos-complements programming environments a negative non-zero ! integer value will always start with a 1 and a positive value with 0 ! as the first bit is the sign bit. Such platforms are very common. end program demo_leadz
> LEADING ZERO BITS= 0 OF VALUE 11111111111111111111111101101010 AKA -150 > LEADING ZERO BITS= 0 OF VALUE 11111111111111111111111110011100 AKA -100 > LEADING ZERO BITS= 0 OF VALUE 11111111111111111111111111001110 AKA -50 > LEADING ZERO BITS=32 OF VALUE 00000000000000000000000000000000 AKA 0 > LEADING ZERO BITS=26 OF VALUE 00000000000000000000000000110010 AKA 50 > LEADING ZERO BITS=25 OF VALUE 00000000000000000000000001100100 AKA 100 > LEADING ZERO BITS=24 OF VALUE 00000000000000000000000010010110 AKA 150
Fortran 2008
BIT_SIZE(3), POPCNT(3), POPPAR(3), TRAILZ(3)
Fortran intrinsic descriptions (license: MIT) @urbanjost
Nemo Release 3.1 | leadz (3fortran) | November 02, 2024 |