Thread: BitFields -- is this possible?

  1. #1
    Registered User
    Join Date
    Nov 2009

    BitFields -- is this possible?

    Hello everyone,
    BitFields gives us the liberty to decide how many bits to allot for the particular field.
    struct x
       unsigned a:1;
       unsigned b:2;
       unsigned c:3;
    struct x x1;
    x1.a=0; //binary is 0
    x1.b=2; // binary is 10
    x1.c=5; // binary is 101
    so we used only 6 bits.
    this means we could accommodate this struct within the size of a char(with char occupying 1 byte i.e 8 bits)

    so does this get stored as 010101 in contiguous memory locations ?
    if so then
    my question here is can we typecast this structure to char and print it with %c or %d as a whole value? i.e decimal of 010101 is 21 and its ASCII equivalent using %c ????


  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    No, there is no guarantee that the individual bitfield values are stored consecutively. In fact, from what I remember it is highly likely that any padding be typically somewhere like just before the second-to-last member.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    You can also make padding (force alignment):
    struct foo
        int a:1;
        int b:2;
        int  :0;
        int c:1;

    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Yes you can, but it's a bit of a mess. For example you need to make sure the struct is actually 1 byte in memory, you will need long casts everywhere. It's probably simpler IMO, to just use and unsigned byte, and access the individual bits with | and & bitwise operators.

  5. #5
    Registered User
    Join Date
    Nov 2009
    but how is this possible .. i mean as far as i know structure variables are stored in contiguous memory locations .. or if not being in contiguous memory locations is the problem and NOTHING ELSE then we can add #pragma pack(1) so as to store the variables in the addresses that are multiples of 1 i.e. in continguous mem loc. but if i am sure that the variables are stored in contiguous mem loc then how should i typecast it to int .. my compiler is not allowing me to do that ..

  6. #6
    Registered User
    Join Date
    Nov 2009
    or is it that we cant typecast structure to integer ???

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    The problem with bit-fields is that there is no way to know (or control) whether you end up with
    within a single storage unit.

    Yes, you can do something like
    memcpy( &myint, &myBitfieldStruct, sizeof(int) );

    and get all the bit fields into a variable, and from there you can print it in whatever form you choose.

    Or the brute-force answer (not guaranteed - google "alignment exception") is
    int *p = (int*)&myBitfieldStruct;
    // now do something with *p
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bitfields
    By Tiago in forum C Programming
    Replies: 2
    Last Post: 05-16-2010, 09:21 AM
  2. Structure Bitfields
    By samGwilliam in forum C Programming
    Replies: 6
    Last Post: 09-04-2009, 09:45 PM
  3. Bitfields, Bit/Little Endian
    By plan7 in forum C Programming
    Replies: 17
    Last Post: 11-08-2007, 01:48 AM
  4. portable bitfields
    By Yarin in forum C++ Programming
    Replies: 2
    Last Post: 08-14-2007, 01:39 PM
  5. bitfields
    By Hunter2 in forum C++ Programming
    Replies: 2
    Last Post: 11-27-2002, 05:21 PM

Tags for this Thread