Thread: bit fields in a structure

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    25

    bit fields in a structure

    Hi all

    I have a structure

    Code:
    struct test {
                      union test1{
                                            short int         Version:4,
                                                                  Ack_Req:1,
                                                                  Ack_Rsp:1,
                                                                  Reserved:10;
                                             short int       var;
                       }test11;
    }test22;
    If I want to fill all those bit fieds (version, Ack_Req, etc), should I use "var" for filling those bit fileds [test22.test11.var = <some value>]

    But even if I want to access them(bit fileds) individually
    [test22.test11.Version=<some value>], I can access, compiler does not give any warning...I'm not sure whether this is the right technique to access those bit fields... because they all are a single member element.

    Please comment....

    thanks & regards
    onebrother

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Firstly, you want to do this to ensure your bit fields do not take up the same bits:
    Code:
    struct test {
                      union test1{
                                            struct{
                                                    short int         Version:4,
                                                                          Ack_Req:1,
                                                                          Ack_Rsp:1,
                                                                          Reserved:10;
                                            };
                                             short int       var;
                       }test11;
    }test22;
    Second, the outermost struct is pointless, unless you have other elements in it besides the union.

    Third, the order of the bit fields is not guaranteed and is compiler dependent. Accessing the struct (containing all the bit fields) and the var member interchangeably yields undefined, compiler dependent, behavior. It is up to the programmer to ensure that this does not happen. If you assign "val" a value, it would then be undefined behavior to get the bits fields (except to assign a value to them, thereby invalidating the previous assignment). Similarly if you assign values to any of the bit fields, then it is undefined behavior to access val for anything but assignment.

    All that said, if you do determine the order of the bit fields that the compiler uses, it is pretty safe to use the union as a converting mechanism. That assumes that you are planning on using just the one compiler.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    All that said, if you do determine the order of the bit fields that the compiler uses, it is pretty safe to use the union as a converting mechanism. That assumes that you are planning on using just the one compiler.
    Not to mention padding or structure alignment, which is likely to be another problem. (I think it applies to bit fields.) And edianness would be an issue too.

    Since you already have the data you want to put into the union in a variable, why not just store the data in the variable and use bit shifts whenever you want to access a given variable? You could create a macro or function to do this. Alternatively, assign each field of the structure individually. It's really the easiest way.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bit level permutation function
    By zxcv in forum C Programming
    Replies: 2
    Last Post: 07-27-2008, 01:26 PM
  2. Struct of bit fields.
    By Kempelen in forum C Programming
    Replies: 5
    Last Post: 06-21-2008, 02:36 PM
  3. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 10:54 AM
  4. Porting from 32 bit machine to 64 bit machine!
    By anoopks in forum C Programming
    Replies: 10
    Last Post: 02-25-2005, 08:02 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM