Interview Question

Interview San Diego, CA

Identify a 5-bit pattern, given a bit stream, accessible by

  function uint8_t getBits(), which returns 4 bits at a time.
Answer

Interview Answer

1 Answer

0

#include<iostream> #include<vector> #include<stdio.h> using namespace std; int numMatches(vector<unsigned char> get_bits, unsigned char pattern){ unsigned char stream = 0x00; int counter = 0; for(int i = 0; i<get_bits.size(); i++){ stream = stream<<4; stream = stream | get_bits[i]; for(int j = 0; j<4; j++){ pattern = pattern << (j>0?1:0); if ((pattern & stream) == pattern){ counter++; } } pattern = pattern >> 3; } return counter; } int main(){ vector<unsigned char> get_bits; get_bits.push_back(0x0F); get_bits.push_back(0x0D); get_bits.push_back(0x0F); get_bits.push_back(0x00); unsigned char pattern = 0x1F; cout<<numMatches(get_bits, pattern); }

Ambu Sreedharan on Jan 16, 2013

Add Answers or Comments

To comment on this, Sign In or Sign Up.