# Moving Bits Question

**URL:** <https://fortran-lang.discourse.group/t/moving-bits-question/4799>\
**Category:** Help\
**Created:** [November 24, 2022, 6:44pm UTC](https://fortran-lang.discourse.group/t/moving-bits-question/4799 "2022-11-24T18:44:21Z")\
**Posts on this page:** 1\
**Showing post:** 31

<div class="post-metadata">

**Author:** ![jeremie.vandenplas](https://avatars.discourse-cdn.com/v4/letter/j/76d3ee/32.png) [@jeremie.vandenplas](https://fortran-lang.discourse.group/u/jeremie.vandenplas)\
**Post date:** [November 27, 2022, 9:33am UTC](https://fortran-lang.discourse.group/t/moving-bits-question/4799/31 "2022-11-27T09:33:04Z")

</div>

> [@kargl](#):
>
> From Fortran 16.3.1:
> 
> ```auto
> The interpretation of a negative integer as a sequence of bits
> is processor dependent.
> 
> ```
> 
> So, `ibset`, `ibclr`, `btest` may or may not do what you want with respect to the sign bit.

@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:

```fortran
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:

```shell
[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?

---

_[View the full topic](https://fortran-lang.discourse.group/t/moving-bits-question/4799)._
