Reputation: 1
I have set of opcodes to carryout a specific function but the tricky part is here: for example in the posted code below, channelABC is the input, which means: if in my product side has channel A, or channel b, or channel c is selected, it should match,OR, if in my product side . if channel b and c are selected it should match, basically if one or more of the Channels match (with input side or product side),- Led has to glow .
i tried to map it, but i am not sure of the right way to do it
typedef enum{
ZoneA = 0x01,
ZoneB = 0x02,
ZoneC = 0x04,
ZoneD = 0x08,
zoneE = 0x10,
ZoneF = 0x20,
ZoneG = 0x40,
ZoneH = 0x80,
ZoneABCD = 0x0f,
ZoneAB = 0x03,
ZoneAC = 0x05,
ZoneAD = 0x09,
ZoneBC = 0x06,
ZoneBD = 0x0A,
ZoneCD = 0x0C,
ZoneABC = 0x07 ,
ZoneABD = 0x0B,
ZoneBCD = 0x0E,
NOZONE = 0x00
}zone;
railzone =buffers[0]; //rail zone read the value , which is the first element in the buffer when the packet info is transformed to buffer
//railzone will have the input here
if(railzone ==ZoneABCD || railzone == ZoneA || railzone == ZoneB || railzone == ZoneC || railzone == ZoneD || railzone == ZoneAB
|| railzone == ZoneAC || railzone == ZoneAD || railzone == ZoneBC || railzone == ZoneBD || railzone == ZoneCD || railzone == ZoneABC ||
railzone == ZoneABD || railzone == ZoneBCD )
{
}
I am giving input as ZONEABC, AND I am having the zoneAB in my product and since two of the zoneA and b are present, it should glow the LED
Upvotes: 0
Views: 75
Reputation: 4115
You can use concept of masks. Define a Mask for which zones your product supports i.e. create a variable and set the bit for every zone your product supports. For example, If your product supports Zone A and Zone C
(Considering your enum)
#define PRODUCT_MASK (ZoneA | ZoneC)
Then sanitize input as
if((railzone_input & PRODUCT_MASK) != 0)
{
// Zone is supported
}
else
{
// Zone is not supported
}
If your railzone_input is ZoneBC (i.e. 6) and as I considered in above example, your PRODUCT_MASK will be 5. So 6 & 5 = 4 which is != 0 i.e. Zone is supported.
If your railzone_input is ZoneB (i.e. 2) then 2 & 5 = 0 which is == 0 i.e. Zone is not supported.
Upvotes: 1
Reputation: 133609
It's not totally clear what you need to do but it looks like you want to carry out a specific operation if some bits of the variable are set, this can be done with &
(and) operator.
For example, if you want to do something on any railzone
which has zone A enabled (bit 0), then you could do
railzone = ZoneAB;
if ((railzone & ZoneA) == ZoneA) {
// turn on led for ZoneA?
}
Upvotes: 0