I have an unsigned char at 6 bytes long. The value being stored within the char is:
Hex: 53167DFD95B7
Binary: 010100 110001 011001 111101 111111 011001 010110 110111
What I need to do is capture bit 1 and bit 6. Then convert that to a decimal. Then capture bit 2-5 and convert that to decimal For example, bit 1 here is 0, bit 6 is 0, so binary 00, is decimal 0. Then for bit 2-5, binary 1010, or decimal 10. Then move to the next group of 6 bits.
Bit 1 is 1, Bit 6 is 1, so binary 11, or decimal 3 Bit 2-5 is binary 1000, or decimal 8
Bit 1 is 0, Bit 6 is 1, so binary 01, or decimal 1 Bit 2-5 is binary 1100, or decimal 12
And so on for the remaining groups of 6 bits.
I'm not really sure how I should be masking, shifting for this. Since this is only 6 bits at a time I am having some difficulty. Any help with this would be greatly appreciated! Thank you all in advance.
EDIT
int getBitVal(unsigned char *keyStrBin, int keyIndex) {
int keyMod = keyIndex % 8;
int keyIn = keyIndex / 8;
return (((keyStrBin[keyIn]) >> (7 - (keyMod))) & 1);
}
void getSValueMajor(char **tableS, unsigned char *f, unsigned char *sValue) {
int i, bitOne, bitSix;
int sCol;
for (i = 0; i < 8; i++) {
bitOne = getBitVal(f, 0);
bitSix = getBitVal(f, 5);
// Do something here to get only bits 2-5. Doesn't matter if its decimal. Just need the 4 bits.
}
}
Ill shift by 6 bits I guess at the end of the loop to go to the next 6 bits, but not sure how to read those 4 bits into a variable.