You can create a mask for any length of the bit pattern. Here is an example of C. This would prevent you from doing hard 0x7 encoding if you want to check for more than three bits.
bool bitPattern(int N, int B) { int shift = 0; int mask = 0x0; while(B >> shift++ > 0) mask |= 0x01 << shift-1; return (N & mask) == B; } int main(int argc, char *argv[]) { printf("01011100 ends with 100 ? %s\n", bitPattern(0x5C, 0x04) ? "Yes" : "No"); printf("01011100 ends with 101 ? %s\n", bitPattern(0x5C, 0x05) ? "Yes" : "No"); }
source share