Comparing random number of bits
Bug

Your computation of last_bits is wrong:


unsigned int last_bits = check_n_bits &lt;= 8 ? check_n_bits : (bytes * 8) - check_n_bits;
last_bits = last_bits == 0 ? 8 : last_bits;



Suppose check_n_bits were 18 as in your example.  This would make bytes 3, and so last_bits would be set to 24 - 18 = 6 instead of 2.  Later, you would check 6 bits in the 3rd byte instead of 2 bits.  This must mean that your unit tests aren't covering enough cases.

Awkward array usage

There are a couple of lines that are really awkward:


sprintf((char *) &amp;tmp_bin_repr, BYTETOBINARYPATTERN, BYTETOBINARY(c));



Here you are casting the address of a character array into a character pointer.  Any time you use a cast, you should think about whether you really need that cast or if you are simply doing something wrong.  In this case, you are doing it wrong.  You can just pass the array like this:

sprintf(tmp_bin_repr, BYTETOBINARYPATTERN, BYTETOBINARY(c));


The second awkward line is this:


tmp_bin_repr_2[j] = *(&amp;(bin_string[i * 8]) + j);



which can be simplified to this:

  tmp_bin_repr_2[j] = bin_string[i*8 + j];


No need for temp copy

Actually I don't think you really even need tmp_bin_repr_2 because all you are doing is making a copy of part of bin_string.  You could just compare with that part of bin_string directly if you modified your call to memcmp().  You can also remove the unnecessary casts as well:

if (memcmp(&amp;tmp_bin_repr[skip_bits_if_last], &amp;bin_string[i*8], bits_to_compare) != 0) {