check this small program, you need to pass the pattern_mask correctly say you want to check the pattern 110 then mask would be 111 i.e. 0x07. Rest is very simple.
int pattern_check(int number, int pattern, int pattern_mask)
{
int count = 0;
while(number > 0) {
if( !((number ^ pattern) & pattern_mask) ) {
++count;
}
number >>= 1;
}
return count;
}
int main(void)
{
unsigned int number = 0xa2;
unsigned int pattern = 0x02;
unsigned int pattern_mask = 0x03;
int count = pattern_check(number, pattern, pattern_mask);
printf("\ncount: %d\n", count);
return 0;
}