Originally Posted by
franziss
So you are saying that for a array element of size 8bits, I can store 4 DNA characters each of size 2 bits, and I need bitwise operations to get the character, but what does these means?
Code:
byte (index/4), bits (index%4)*2 and (index%4)*2 +1.
Picture it in your mind...say you read in 3 bytes. If the bits were laid out in the array so that the first base was stored in the least significant 2 bits, the second base was stored in the next least significant bits, etc., it might look like this:
Code:
10110101 11001101 01000110
\/\/\/\/ \/\/\/\/ \/\/\/\/
3 2 1 0 7 6 5 4 11109 8
That's 12 different bases, or whatever. 2 bits for each one. The first byte would be at index array[0], the next at array[1], and the last at array[2]. So say you had a variable called index that stored which base you wanted. You could retrieve it like this:
Code:
base = (array[index / 4] >> ((index % 4) * 2)) & 3;
And you could store 2 bits at any given index like this:
Code:
array[index / 4] |= base << ((index % 4) * 2);
So if index was 0 it would grab the 2 least significant bits of array[0]. If index was 6 it would grab the pair of bits that was second to most significant of array[1] (6 / 4 = 1, and (6 % 4) * 2 = 4).
Clear as mud, right?
EDIT: Doh! Beaten to the punch.