Is there a more compact way of comparing my bits than this (the only way I know):
#define BIT1 1
#define BIT2 2
#define BIT3 4
#define BIT4 8
#define BIT5 16
#define BIT6 32
// I declare this somewhere in a structure
unsigned char bits: 6;
// I want all of them to be 0 at first (000000)
bits = 0;
/* I do some bite setting here */
// I only want to know if the state of my bits == 000000
if(bits & (BIT1 | BIT2 | BIT3 | BIT4 | BIT5 | BIT6) == (BIT1 | BIT2 | BIT3 | BIT4 | BIT5 | BIT6))
{
// All kinds of nasty stuff
}
I thought maybe something in the lines of bits & 0x00 == 0x00
If you want compact (as indicated in your comment) rather than fast, why not do something like:
It probably won’t be any faster since your original code would have been turned into that constant anyway but it may be more readable (which is often a good reason to do it).
If you have a need for other variations, just define them. If there’s too many of them (63 defines, if you use them all, may be getting a bit on the high side), I’d start thinking about another solution.
But, to be honest, unless you’re going to use more meaningful names for the defines, I’d just ditch them. The name
BIT3really adds nothing to0x04to those that understand bit patterns. If it was something like UART_READ_READY_BIT, that would be fine but what you have is only slightly better than:(no offence intended, I’m just pointing out my views). I’d just work out the bit patterns and put them straight in the code (bits 1 thru 6 in your case being 0x3f).
And, just as an aside, for you particular case, I think
bitswill only be those six bits anyway so you may find it’s enough to compare it to 0 (with no bit masking). I’ve left in the bit masking method in case you wanted a mode general solution for checking specific bits.