Checking the bits for a numeric position is one of the right ways to do this, but it makes the code depend on magic numbers, which makes it difficult to read and maintain.
, , , , , .
, , , , :
enum LightMask {
ENTRANCE = 0x01,
LIVING_ROOM = 0x02,
RESTROOM = 0x04,
KITCHEN = 0x08,
BATHROOM = 0x10,
BEDROOM1 = 0x20,
BEDROOM2 = 0x40,
ATTIC = 0x80
};
uint8_t lightsOn = GetLightsOn();
if (lightsOn & (BATHROOM | KITCHEN)) {
// ...
}
, .
, :
enum LightMask {
ENTRANCE = 1 << 0,
LIVING_ROOM = 1 << 1,
RESTROOM = 1 << 2,
KITCHEN = 1 << 3,
BATHROOM = 1 << 4,
BEDROOM1 = 1 << 5,
BEDROOM2 = 1 << 6,
ATTIC = 1 << 7
};