linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Randi Botse <nightdecoder@gmail.com>
To: linux-c-programming@vger.kernel.org
Subject: Re: Help on bit operation
Date: Sat, 29 Aug 2009 01:40:56 +0700	[thread overview]
Message-ID: <34e1241d0908281140t65d6eb8cq371ce68cefc2f531@mail.gmail.com> (raw)
In-Reply-To: <34e1241d0908281139k66e8102dy94b70e9fcfd86763@mail.gmail.com>

Ben, after assign all bitfield struct member, how to pack them to be a
32bit integer value?

DEC      BIN
43        101011        -> foo.a (6 bit)
11        1011            -> foo.b (4 bit)
120      01111000    -> foo.c (8 bit)
30        11110          -> foo.d (5 bit)
418      110100010  -> foo.e (9 bit)

the bit pattern is : 10101110110111100011110110100010 or 2933800354 in
decimal, is this possible?

while with masking and shift i can pack them with:
((foo.a & 0x3f) << 26) | ((foo.b & 0xf) << 22) | ((foo,c & 0xff) <<
14) | ((foo.d & 0x1f) << 9) | (foo.e & 0x1ff)


On Sat, Aug 29, 2009 at 12:02 AM, Ben Rosenberg<scdlbx@gmail.com> wrote:

> 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 majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

  parent reply	other threads:[~2009-08-28 18:40 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-08-26 23:02 Help on bit operation Randi Botse
2009-08-26 23:33 ` Akos Marton
2009-08-26 23:37 ` Uday Verma
2009-08-26 23:45 ` Ben Rosenberg
2009-08-27  0:15 ` Glynn Clements
2009-08-27  6:43   ` Randi Botse
2009-08-28  4:52     ` Randi Botse
2009-08-28 17:02       ` Ben Rosenberg
     [not found]         ` <34e1241d0908281139k66e8102dy94b70e9fcfd86763@mail.gmail.com>
2009-08-28 18:40           ` Randi Botse [this message]
2009-08-28 19:58             ` Ben Rosenberg
2009-08-28 20:14               ` Tim Walberg
2009-08-28 20:19             ` Glynn Clements
2009-08-29  7:40               ` Randi Botse

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=34e1241d0908281140t65d6eb8cq371ce68cefc2f531@mail.gmail.com \
    --to=nightdecoder@gmail.com \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).