user3382401
user3382401

Reputation: 43

small integers in fortran

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

Answers (2)

Glenn Randers-Pehrson
Glenn Randers-Pehrson

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

M. S. B.
M. S. B.

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

Related Questions