POPPAR(3) - [BIT:COUNT] Parity of the number of bits set
result = poppar(i)
elemental integer function poppar(i)integer(kind=KIND), intent(in) :: i
o I is an integer of any kind o the return value is a default kind integer
POPPAR(3) returns the parity of an integers binary representation (i.e., the parity of the number of bits set).
The parity is expressed as
o 0 (zero) if I has an even number of bits set to 1. o 1 (one) if the number of bits set to one 1 is odd,
o I : The value to query for its bit parity
The return value is equal to 0 if I has an even number of bits set and 1 if an odd number of bits are set.
Sample program:
program demo_poppar use, intrinsic :: iso_fortran_env, only : integer_kinds, & & int8, int16, int32, int64 implicit none character(len=*),parameter :: pretty=(b64,1x,i0) ! basic usage print pretty, 127, poppar(127) print pretty, 128, poppar(128) print pretty, int(b"01010"), poppar(int(b"01010"))Results:! any kind of an integer can be used print pretty, huge(0_int8), poppar(huge(0_int8)) print pretty, huge(0_int16), poppar(huge(0_int16)) print pretty, huge(0_int32), poppar(huge(0_int32)) print pretty, huge(0_int64), poppar(huge(0_int64)) end program demo_poppar
> 1111111 1 > 10000000 1 > 1010 0 > 1111111111111111111111111111111 1 > 1111111 1 > 111111111111111 1 > 1111111111111111111111111111111 1 > 111111111111111111111111111111111111111111111111111111111111111 1
Fortran 2008
There are many procedures that operator or query values at the bit level:
POPCNT(3), LEADZ(3), TRAILZ(3) ATOMIC_AND(3), ATOMIC_FETCH_AND(3), ATOMIC_FETCH_OR(3), ATOMIC_FETCH_XOR(3), ATOMIC_OR(3), ATOMIC_XOR(3), BGE(3), BGT(3), BIT_SIZE(3), BLE(3), BLT(3), BTEST(3), DSHIFTL(3), DSHIFTR(3), IALL(3), IAND(3), IANY(3), IBCLR(3), IBITS(3), IBSET(3), IEOR(3), IOR(3), IPARITY(3), ISHFTC(3), ISHFT(3), MASKL(3), MASKR(3), MERGE_BITS(3), MVBITS(3), NOT(3), SHIFTA(3), SHIFTL(3), SHIFTR(3), STORAGE_SIZE(3)
Fortran intrinsic descriptions (license: MIT) @urbanjost
Nemo Release 3.1 | poppar (3fortran) | November 02, 2024 |