I would describe my question using an example
Given a source - 1010 0010, I would like to know how many times (count) the pattern 10 is present in the byte (the source can be of any size 8, 16, 24, or 32 bits).
I would like the function which finds the count to be generic. User should be able to give his own pattern viz. 1000, 101 etc
I want to add that I tried solving the problem. Below is the code snippet (In C Language)
The logic that I have used is to use Ex-OR operation so that if the pattern matches the result of ex-or operation will be 0.
unsigned int FindPattern (unsigned int u32Number, unsigned int u32Pattern)
{
unsigned int count = 0;
unsigned int u32Temp = 0;
while (0 != u32Number)
{
/* How can I turn off (0) all the bits except bits which represent pattern
* For example if pattern is 3 bits then the all the bits except the last 3
* bits should be 0. */
if(!(u32Number ^ u32Pattern))
{
count++;
}
u32Number = u32Number >> 1;
}
return count;
}