> Hello all,
> 
> Another Question on C/C++, Please be patient just learning.
> 
> Example from Palm OS Bible librarian sample.
> 
> librarianDB.c
> 
> // Utility macros for handling bit fields
> 
> #define BitAtPosition(pos) ((UInt16)1 << (pos))
> 
> #define GetBitMacro(bitfield, index) ((bitfield) & BitAtPosition(index))
> 
> #define SetBitMacro(bitfield, index) ((bitfield) |= BitAtPosition(index))
> 
> #define RemoveBitMacro(bitfield, index) ((bitfield) &= 
> ~BitAtPosition(index))
> 
> Here's the question in my newfound C speak;
> 
> if ((#define BitAtPosition(pos) ((UInt16)1 << (pos))) == ("Sets a 16 bit 
> bitmask with (pos)")) {
> 
>    " Why not declare bitmap constants?";
> 
>     "Is there a performance issue?";
> 
>     a memory space issue?;
> 
>     "Or just to avoid constants for the many flags in this app?";
> 
>   }
> 
> else {
> 
>   "Did I miss the whole point of the Macro?";
> 
> "I hope not given the time it took to come up with the interpretation";
> 
> }
> 

Whether to use macros like this or use bitmap constants depends on
what you're wanting to do in your program. I'd use this kind of thing
to set up a bit mask where I had, say, five things I wanted to
reference in a bitmask, and wanted to iterate over them, so the code
would be something like:

for (k=0; k<5; k++)
    if (GetBitMacro(bitfield, k))
    {
      //Do something
    }

On the other hand, if I wanted to set up a bit mask where I had five
dissimilar items, and there would never be a need to iterate over
them, I'd probably use bitmap constants. An example of this would
probably be option flags passed in to a program or function, where
each option would require different handling.

Dean

-- 
For information on using the PalmSource Developer Forums, or to unsubscribe, 
please see http://www.palmos.com/dev/support/forums/

Reply via email to