linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ben Rosenberg <scdlbx@gmail.com>
To: Randi Botse <nightdecoder@gmail.com>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: Help on bit operation
Date: Fri, 28 Aug 2009 10:02:04 -0700	[thread overview]
Message-ID: <ca4725240908281002h4fc32b39t23fb84518bd0ab6a@mail.gmail.com> (raw)
In-Reply-To: <34e1241d0908272152r32591e52wb1885433edb9a9ff@mail.gmail.com>

On Thu, Aug 27, 2009 at 9:52 PM, Randi Botse<nightdecoder@gmail.com> 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 majordomo@vger.kernel.org
> 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 majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

  reply	other threads:[~2009-08-28 17:02 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 [this message]
     [not found]         ` <34e1241d0908281139k66e8102dy94b70e9fcfd86763@mail.gmail.com>
2009-08-28 18:40           ` Randi Botse
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=ca4725240908281002h4fc32b39t23fb84518bd0ab6a@mail.gmail.com \
    --to=scdlbx@gmail.com \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=nightdecoder@gmail.com \
    /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).