@kargl I am a bit confused with this statement in Fortran 16.3.1.
E.g., with the intrinsic ibits
, I would expect that ibits(val, 6, 2)
always return the 2 same bits for an integer(int8) :: val
read from a stream
binary file, whatever the value of val
is. For example:
program test_r
use, intrinsic :: iso_fortran_env, only: int8
implicit none
integer :: un
integer(int8) :: dummy
open(newunit=un, file='test.stream', access='stream', action='read')
read(un) dummy
close(un)
write(*,'(1b2.2)')ibits(dummy, 0, 2)
write(*,'(1b2.2)')ibits(dummy, 2, 2)
write(*,'(1b2.2)')ibits(dummy, 4, 2)
write(*,'(1b2.2)')ibits(dummy, 6, 2)
end program
Example:
[test_ibits]$ xxd -b test.stream
00000000: 10010001 .
[test_ibits]$ gfortran test_r.f90
[test_ibits]$ ./a.out
01
00
01
10
The file test.stream
could be created by another program (even another language). What I need, it is to extract the different bits of the int8
value (each 2 bits having a different meaning). But the interpretation of the 8-bit sequence as read from the file (i.e. the value of the int8
integer) is not relevant to me, neither the interpretation of the 8-bit sequence returned by ibits
(i.e. the value of the returned int8
integer; in this case always a positive integer).
Based on your warnings, should I understand that the results of the program above cannot be trusted (across different processors) if the interpretation of the 8-bit sequence in the file test.stream
is a negative integer?