I have a short variable (16 bits) and an index (unsigned char).
I need a macro that returns the index
th in my variable data
.
This is what I got:
#define GETBIT(data, index) data & 1 << index
And how I use it:
unsigned char i;
short * twobytes = (short *) calloc(1, sizeof(short));
twobytes = ((char * )buffer + *currentIndex);
while (codeLength != 0)
{
i = GETBIT(code, codeLength--);
*twobytes = SETBIT(*twobytes, *currentBitIndex, i);
(*currentBitIndex)++;
if (*currentBitIndex == 8) {
(*currentIndex)++;
(*currentBitIndex) %= 8;
}
}
For some reason i
always equals to 0
in my test cases, where it sometimes should equal 1
.
What am I doing wrong and how should I fix it?
Thanks.