To explain the macro: ~(x) is to flip the bits of the value X (1's complement). For example, if X was 0110, then ~(x) will be 1001. This is so that a switch, when turned ON, shorts the digital channel to ground reading a 0. So ON becomes zero, and OFF becomes 1. For our BCD swtich, we need ON as 1 and OFF as 0, so we use ~(x) to flip the bits. For the digital input channels, channel 1 is actually bit 15, channel 2 bit 14, channel 3 bit 13 ..., channel 11 is bit 5, channel 12 is bit 4, channel 13 is bit 3 and channel 14 is bit 2. So to right justify bit 11-14, we need to shift it right 2 bits. And of course finally mask it off so we only care about the least significant 4 bits.
Code:
#define BCDSwitchValue(x) (((~(x)) >> 2) & 0x0f)
BTW, the single statement and the macro are totally procedural, no class required
