Re: Help on bit operation

Ben Rosenberg <[email protected]>
Newsgroups org.kernel.vger.linux-c-programming
Message-ID <[email protected]>
On Thu, Aug 27, 2009 at 9:52 PM, Randi Botse<[email protected]> wrote:
> Hi again.
>
> I was able to solve my bit packing/unpacking problems with bit mask
> and shift operations, but i have no idea how to do this using a
> bit-field structure, can someone give me a example?
>
> Thanks
>             - Randi
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to [email protected]
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>

Glynn's example of bitfields is cleaner and better than this, but I
put a basic implementation of bitfields using your parameters at the
end of this message. From my understanding, the way bitfields are
implemented in C is with bit shifts and masks, so I don't think there
is a performance difference between the two.

#include <stdio.h>

struct bitfield {
    unsigned int a:6;
    unsigned int b:4;
    unsigned int c:8;
    unsigned int d:5;
    unsigned int e:9;
};

int main(void) {
    struct bitfield foo;
    foo.a =  43;
    foo.b =  11;
    foo.c = 120;
    foo.d =  30;
    foo.e = 418;
    printf("%u %u %u %u %u\n",foo.a,foo.b,foo.c,foo.d,foo.e);
    return 0;
}


Ben
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.