fortranintel-fortran

# How to get low word and high word from a real?

For data type 32bit reals in fortran, how could you move the first and last 16 bits into integer values? I've tried a number of methods, but can only figure this out if the input is of integer type. Fortran seems to have a limitation working with real types.

example:

decimal 1.0 in binary is : 0011 1111 1000 0000 0000 0000 0000 0000

set HIGH_WORD = 0011 1111 1000 0000

set LOW_WORD = 0000 0000 0000 0000

``````SUBROUTINE TEST(INPUT, LOW, HIGH)

REAL*4 INPUT
INTEGER*4 LOW
INTEGER*4 HIGH

! METHOD1: ONLY WORKS ON INTS
HIGH = IBITS(INPUT, 0, 16)
LOW = IBITS(INPUT, 16, 16)

! METHOD2: ALSO ONLY WORKS ON INTS
HIGH = JISHFT(HIGH, -16)

END

``````

Solution

• The `TRANSFER` intrinsic transfers physical representation:

``````  use, intrinsic :: iso_fortran_env, only : real32, int16

implicit none

integer(int16) :: integer(2)
real(real32) :: real

real = 1.0_real32
integer = transfer(real, integer)

print '(B32.32)', real
print '(B16.16)', integer
end
``````

Transferring of physical representation can include, as in this case, carving a scalar up into smaller individual array elements.