If anyone seeks bit handling in Fortran with type punning in the most unfortunate absence of a full-featured bit string facility in the language, the broken-record-of-a- suggested-approach remains using the interoperable types and intrinsic module procedures in the standard:
use iso_c_binding, only: int8 => c_int8_t, int16 => c_int16_t, &
int32 => c_int32_t, int64 => c_int64_t, &
c_loc, c_f_pointer
logical, parameter :: IS_BIG_ENDIAN = iachar( c=transfer(source=1,mold="a") ) == 0
integer( int8), pointer :: i8(:)
integer(int16), pointer :: i16(:)
integer(int32), pointer :: i32(:)
integer(int64), target :: i64
call c_f_pointer( cptr=c_loc(i64), fptr=i8, shape=[ storage_size(1_int64)/storage_size(1_int8) ] )
call c_f_pointer( cptr=c_loc(i64), fptr=i16, shape=[ storage_size(1_int64)/storage_size(1_int16) ] )
call c_f_pointer( cptr=c_loc(i64), fptr=i32, shape=[ storage_size(1_int64)/storage_size(1_int32) ] )
i64 = 1
print "(g0,*(b64.64))", "i64: ", i64
if ( IS_BIG_ENDIAN ) then
print "(g0,*(b32.32))", "i32: ", (i32(i), i=1,size(i32),1)
print "(g0,*(b16.16))", "i16: ", (i16(i), i=1,size(i16),1)
print "(g0,*(b8.8))", "i8: ", (i8(i), i=1,size(i8),1)
else
print "(g0,*(b32.32))", "i32: ", (i32(i), i=size(i32),1,-1)
print "(g0,*(b16.16))", "i16: ", (i16(i), i=size(i16),1,-1)
print "(g0,*(b8.8))", "i8: ", (i8(i), i=size(i8),1,-1)
end if
print "(g0,z0)", "Address of i64: ", transfer(c_loc(i64), mold=1_int64)
print "(g0,z0)", "Address of i32: ", transfer(c_loc(i32), mold=1_int64)
print "(g0,z0)", "Address of i16: ", transfer(c_loc(i16), mold=1_int64)
print "(g0,z0)", "Address of i8: ", transfer(c_loc(i8), mold=1_int64)
end
C:\temp>gfortran p.f90 -o p.exe
C:\temp>p.exe
i64: 0000000000000000000000000000000000000000000000000000000000000001
i32: 0000000000000000000000000000000000000000000000000000000000000001
i16: 0000000000000000000000000000000000000000000000000000000000000001
i8: 0000000000000000000000000000000000000000000000000000000000000001
Address of i64: C8CB1FF7C8
Address of i32: C8CB1FF7C8
Address of i16: C8CB1FF7C8
Address of i8: C8CB1FF7C8