Like Tree1Likes
  • 1 Post By Subsonics

BitFields -- is this possible?

This is a discussion on BitFields -- is this possible? within the C Programming forums, part of the General Programming Boards category; Hello everyone, BitFields gives us the liberty to decide how many bits to allot for the particular field. ex. Code: ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    43

    BitFields -- is this possible?

    Hello everyone,
    BitFields gives us the liberty to decide how many bits to allot for the particular field.
    ex.
    Code:
    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 ????

    Regards.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    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
    Posts
    14,826
    You can also make padding (force alignment):
    Code:
    struct foo
    {
        int a:1;
        int b:2;
        int  :0;
        int c:1;
    };

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    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.
    iMalc likes this.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    43
    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
    Posts
    43
    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
    Location
    The edge of the known universe
    Posts
    32,852
    The problem with bit-fields is that there is no way to know (or control) whether you end up with
    abbcccxx
    or
    xxabbccc
    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
    Code:
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 10:21 AM
  2. Structure Bitfields
    By samGwilliam in forum C Programming
    Replies: 6
    Last Post: 09-04-2009, 10: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, 02:39 PM
  5. bitfields
    By Hunter2 in forum C++ Programming
    Replies: 2
    Last Post: 11-27-2002, 05:21 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21