David Brown wrote:
> 
> Hi,
> 
> I'm using bitfields more and more these days - they are much more
> debugger-friendly than the traditional #define's and flag bytes, and let the
> compiler do more useful error-checking.
> 
> However, one thing that would make them much nicer would be more efficient
> code for assignment.  For example,
> 
> #define true 1
> #define false 0
> typedef unsigned char byte;
> typedef union {
>     struct {
>         byte a : 1; byte b : 1; byte c : 1; padding : 5;
>     };
>     byte raw;
> } tFlags;
> 
> tFlags x, y;
> 
> void test1(void)
> {
>     x.b = y.c;
> }
> 
> void test2(void)
> {
>     if (y.c) x.b = true; else x.b = false;
> }
> 
> The two functions test1() and test2() have the same efffect, except that
> test1() generates a range of shifts and masking operations, whereas test2()
> uses a smaller and faster arrangement of bit tests and jumps.  In some
> cases, the direct bit manipulation is best, such as when the same bit is
> accessed in two structures.  But would it be possible to automatically
> generate the jump form when that is more efficient?
> 
How about

void test3(void)
{
        x.b = (y.c == true);
}

Garst

Reply via email to