Say I have only two flags (bCold and bHot) that are getting set. I’ve discovered what all possible combinations should equal. How then can I determine what the original (or compatible) constants would be from the below?
When bCold and bHot are both turned ON = 0x4100
When bCold and bHot are both turned OFF = 0x8200
If bCold is ON and bHOT is OFF = 0x8100
If bCold is OFF and bHOT is ON = 0x4200
Knowing the above, what should I set bCold and bHot to equal?
#define bCold ((ULONG)0x???)
#define bHot ((ULONG)0x???)
// Turn them on sometime later
long lCONFIG_FLAGS = bCold | bHOT;
Let’s say that
0bXXXXXXXYmeans binary where Y is the less significant bit.Assuming the result is set with bitwise operations:
Your numbers are made of two bytes. The right (less significant) byte is always 0b00000000, since all numbers end with 00. Lets look at the left (more significant) byte:
From this you can see that the two left-most bits set the bHot, and the two rightmost bits set the bCold (right = less significant).
Now, add the right byte, which we said is always zero, and you get
The result is set by bitwise "OR"
Assuming the result is set by simply adding numbers:
(which is wrong, because your post name include the bitwise OR mention, but still let’s try it just for fun)
A simple equation will show us these figures:
The result could be set by simply adding the numbers, e.g.
0x0200 + 0x8000 = 0x8200for both OFF.Conclusion
As you can see, so the final result is: