Reputation: 43
I need to create a large array of integers in fortran which contains only 1 and -1. Could someone please suggest me how to define such an array that takes minimum possible memory space?
Thank you
Upvotes: 2
Views: 143
Reputation: 12465
If you convert all of your -1 to 0 then you only need one bit per member of your array. Given such a transformed array that contains only 1 and 0, it's not hard to pack it into 32-bit or whatever size integers you like.
Upvotes: 1
Reputation: 29391
An easy approach:
use ISO_FORTRAN_ENV
integer (INT8), dimension (N) :: array
That will give you byte-sized integers. (Unless the compiler actually chooses to implement them with larger integers, which seems implausible but is probably allowed.)
If the array is truly huge and won't fit into storage, you could access bits within variables, but that would not be a simple array.
Upvotes: 2