Thread: Struct of bit fields.

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    147

    Struct of bit fields.

    When coding a program to use struct of bit fields, i.e.

    Code:
    struct m {
    f1:5;
    f2:6;
    f3:7;
    f4:1;
    }
    1) is there any way to fast initialize, i.e. to 0, or must I put each field to 0; (m=0, or, m.f1=0, m.f2=0, etc)....

    2) can I compare structs in the way :
    if (m==...)
    or the only way to compare is field to field?

    thx

  2. #2
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    1. Use typedef struct instead. They will be automatically initialized.
    2. Comparing by field to field is the only way T_T...

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you give an initializer list, all not-listed objects are initialized to zero. You can't use an assignment statement like foo=0. (You could, if you needed to, make a struct m called zero (say) that had all fields zero, and then you could do foo=zero.)

    You can only use == with numbers and pointers.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by audinue View Post
    1. Use typedef struct instead. They will be automatically initialized.
    Typedef is irrelevant here.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    To initialize every member of the structure to zero, you could probably use memset(), though I'm not sure how standard that would be.
    [edit] Global and static variables are automatically initialized to all zeros, but don't use them just for that reason . . . . [/edit]

    It would probably be best to create a function that compares those structures if you do it often.
    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.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    One solution I've seen before is to do this:
    Code:
    struct m {
    union {
      struct bits {
        int f1:5;
        int f2:6;
        int f3:7;
        int f4:1;
      };
    // alt 1:
      struct all {
        int word:19;
      }
    // alt 2:
       int all;
    };
    When you want "all" of the data, you use the all.word (or all field if you choose alternative 2). e.g, using alt2:.
    Code:
    void somefunc()
    {
       m var;
       var.all = 0;
       ...
    // You will also need to use the .bits prefix to set the differnet bitfields:
    
       var.bits.f1 = 7;
       var.bits.f4 = 1;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  2. union in struct with bit fields
    By sagarnewase in forum C Programming
    Replies: 4
    Last Post: 05-12-2008, 07:30 AM
  3. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 10:24 AM