I have a memory-critical FORTRAN program, which at the moment uses two integer arrays, the first uses 4 BYTES integers and the second uses 1 BYTE integers.
Neither array is "fully" used, i.e. I know that the number stored in the 4 byte array won't exceed a million or so (so I need more than 2 bytes, but I'm not using all the bits of 4 bytes) and likewise I only use 2 bits of the 1-byte array as "flags".
To save memory I wanted to try and combine the two, i.e. use the two left-most bits of the 4-byte array to store the 2 flags.
Now I know if I set the INTEGER value first, I can subsequently set the flags using IBSET/IBCLR, and still extract my integer safely using the IBITS function to extract the rightmost X bits (X=28 in this case):
integer(kind=4) :: imark,inum
imark=600245 ! example number
! use bits 29 and 30 to store flags...
imark=IBSET(imark,29)
imark=IBSET(imark,30)
print *,imark ! gives 1611212981
! extract right most bits:
inum=IBITS(imark,0,28)
print *,inum ! gives back 600245 as desired.
However, I don't know how to modify the integer, while retaining the memory of the flags. In other words, I need the opposite function to IBITS, to copy an integer assigning only the X rightmost bits.
Obviously if I assign an new integer value (imark=343525) it will reset the flag bits. I could write a function that cycles through the rightmost X bits of the new integer value one at a time and then sets them using IBSET/IBCLR in my 4-byte array, but that seems very long-winded. But I do not find any intrinsic function to perform this task.