I need to work with a large array of 26-bit variables in RAM. It is too expensive to use 32-bit int
s. Access should be as fast as possible (especially read operation).
I came to the following scheme: each 26-bit value splits to three 8-bit values and one 2-bit value.
#define N 500000000
uint8 arr1[N], arr2[N], arr3[N];
uint8 arr4[N / 4];
int read_value(int index)
{
int a1 = arr1[index]; // bits 0..7
int a2 = arr2[index]; // bits 8..15
int a3 = arr3[index]; // bits 16..23
int a4 = (arr4[index / 4] >> (2 * (index % 4))) & 3; // bits 24..25
return a1 | (a2 << 8) | (a3 << 16) | (a4 << 24);
}
Is there some better technique to do this? Or maybe there is a nice way to work with 27/28/29/30-bit integers?