I am trying represent a bunch of values in a single char: 6 on/off flags in the first 6 bits, and using the last 2 bits to hold 4 different values.
This seems so basic that macros like this must exist somewhere, but I can’t find them.
#define HOT 0x00
#define BIG 0x01
#define FAT 0x02
#define HIT 0x03
#define BAT 0x04
#define ZAX 0x05
#define HOW 0x06
#define TWO_BITS nnn // ???
#define CONDITION_0 nnn // bit combo: 00
#define CONDITION_1 nnn // bit combo: 01
#define CONDITION_2 nnn // bit combo: 10
#define CONDITION_3 nnn // bit combo: 11
void bitter(void)
{
unsigned char myBits = 0;
bool nonsense;
if (myBits & BIG) nonsense = true; // BIG flag on
if (!(myBits & BAT)) nonsense = false; // BAT flag off
myBits = myBits | BIG; // turn on BIG bit
myBits = myBits & ~BIG; // turn off BIG bit
if (TWO_BITS == CONDITION_0)
doThis();
else if (TWO_BITS == CONDITION_1_)
doThat();
// ... etc
}
So what is the best way to code what I want to do with those last 2 bits?
I haven’t been able to do any performance testing because I haven’t figured out how to write the code, but I’m assuming this is the fastest way to do these sort of ops.
[btw, this may smell like homework, but I’m just a 54 year old dog trying to learn some new tricks.]
It sort of depends on whether you need to deal with those 2 bits on its own, as a (decimal) value between 0 and 3, or if you always treat them as the upper 2 bits of a byte.
Here’s one way, we just mask out all the other bits, and define the conditions as the value those 2 bits will make as the upper 2 bits in a byte.
That is, the upper 2 bits of a byte is the binary 1 1 0 0 0 0 0 0 , which is 0xC0 in hex. And the upper 2 bits being 0 1 , i.e. with all the bits in a byte it would be 0 1 0 0 0 0 0 0 , which is 0x40 in hex.
And your test would have to be
The other approach is to extract those upper 2 bits as a 2 bit integer (that is, a value between 0 and 3). That’s easy, just shift the bits 6 places to the right.
The usage would be the same when testing one of the conditions.
A last note, it seems your lower 6 bit flags is a bit wrong, consider e.g.
0x06 is the binary value 0 0 0 0 0 1 1 0 , so that’s actually turning on or testing for 2 bits. You likely want to associate 1 bit with 1 flag, which would be this sequence
This is often written as a bit shift so it’s easy to read which bit it is: