Following the previous discussion on this post, I would like to raise more discussion on bit manipulation/representation of real numbers.
What I would like to do is to put the log version of integer vec N = [1, 2, 3, 4], i.e., log2(N), into an array of real numbers. I wish to put it at the last few bits of mantissa.
My goal is to sort this array of real numbers and retrieve the index directly from the sorted array, therefore we need to retrieve those last few bits of mantissa back again.
One way to retrieve the bits that make up a real number is to use the transfer function. It will copy the contents of the input variable into the result without any further interpretation.
Well, here is a small, silly, program to illustrate the procedure:
! bitreal.f90 --
! Go from real to bits to real
!
program bitreal
use iso_fortran_env
implicit none
real(kind=real64) :: x
integer(kind=int64) :: y
x = 0.73980422705616400_real64 ! Note the kind!
write(*,*) x
y = transfer( x, y )
write(*, '(b64)') y
y = lshift( rshift(y,10), 10 )
write(*, '(b64)') y
x = transfer( y, x )
write(*,*) x
end program bitreal
Do note that you have to specify the kind of the literal number. In your example it is a single-precision real.
Thank you so much! I think now I have a better understanding.
May I ask more about bit manipulation? For example, I transfer the real array (double-precision) into an integer array, with the index array also is integer (also double-precision?). How could I incorporate the index array into the last few bits of mantissa? I tried the ibset and I guess if I did it four times it will probably work, but I just wonder whether mvbits or other bit manipulation function can elementally map it to the last few bits of mantissa.
I am no expert in the use of these functions, so I can only guess. Note that your real number is single precision! That could mean a lot of zero bits at the end.