If the bits are equal to each other then they must either all be equal to 0 or all be equal to 1. The following C code works for comparing any number of bits.

Expand|Select|Wrap|Line Numbers

#include <stdbool.h>

bool isMatchingBits(unsigned long value, unsigned long mask) {