Rich Teer wrote: > On Wed, 11 Jun 2008, Garrett D'Amore wrote: > > >> I recall when NetBSD added them about year or two ago. It isn't that >> clear to me that they serve that much use, or really help clear code up >> that much. But I'll defer to consensus. >> > > In my current day job (writing code for embedded systems), I'm accessing > bits of registers quite frequently. I find macros like this very useful; > for example: > > #define BITVALUE(bit) (1UL << (bit)) > #define MY_MASK (BITVALUE (4)) > > is much more readable than > > #define MY_MASK (1UL << 4) >
I have macros like those above, yes. (Although in my code it looks more like this:) #define BIT(x) (1UL << (x)) ... #define MYMASK BIT(4) Note the elimination of an extra set of parens.) But this is all little nits. One thing is that BIT(x) can't be used directly, so you wind up with something like __BIT(x) or somesuch to avoid namespace collision. But the value added by BIT(x) is fairly small, IMO, because normally you wind up with a lot of similar masks for different register bit definitions: #define MYBITA (1UL << 0) #define MYBITB (1UL << 1) #define MYBITC (1IUL << 2) The BIT(x) macro makes it a tiny bit more legible, but not a great deal. > And perhaps more meaningful than > > #define MY_MASK (16) > Yes, I do prefer it this way. I hate defining constants that are really bit positions this way. (And yet, sometimes, I even do that. But usually that's only when I have a range of bits to deal with.) What I'm not sure about is the __SHIFTIN, __SHIFTIN, and __BITS(a,b) forms that jmcp referred to. I don't see a lot of value in them -- possibly because I don't use them enough that they are automatically familiar and obvious to me. -- Garrett _______________________________________________ opensolaris-code mailing list opensolaris-code@opensolaris.org http://mail.opensolaris.org/mailman/listinfo/opensolaris-code