This may be a little theoretical question. I have a w980 byte array containing network packets. I want to check for the presence of each pair of bits ('01' or '10') every 66 bits. That is, as soon as I find the first pair of bits, I must skip 66 bits and check again for the presence of the same pair of bits. I am trying to implement a program with masks and shifts, and it is quite difficult. I want to know if anyone can suggest a better way to do the same.
The code I wrote so far looks something like this. However, it is not complete.
test_sync_bits(char *rec, int len) { uint8_t target_byte = 0; int offset = 0; int save_offset = 0; uint8_t *pload = (uint8_t*)(rec + 24); uint8_t seed_mask = 0xc0; uint8_t seed_shift = 6; uint8_t value = 0; uint8_t found_sync = 0; const uint8_t sync_bit_spacing = 66; target_byte = *(uint8_t*)(pload + offset); while(seed_shift) { value = ((target_byte & seed_mask) >> seed_shift); if((value == 0x01) || (value == 0x10)) { save_offset = offset; found_sync = 1; break; } else { seed_mask = (seed_mask >> 2) ; seed_shift-=2; } } offset = offset + 8; seed_shift = (seed_shift - 4) > 0 ? (seed_shift - 4) : (seed_shift + 8 - 4); seed_mask = (seed_mask >> (6 - seed_shift)); }
Another idea that I came up with was to use the structure defined below
typedef struct { int remainder_bits; int extra_bits; int extra_byte; }remainder_bits_extra_bits_map_t; static remainder_bits_extra_bits_map_t sync_bit_check [] = { {6, 4, 0}, {5, 5, 0}, {4, 6, 0}, {3, 7, 0}, {2, 8, 0}, {1, 1, 1}, {0, 2, 1}, };
Is my approach right? Can anyone suggest any improvements for the same?