Thread: bitfields

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    bitfields

    Suppose I did this:
    Code:
    struct Stuff
    {
        var : 1
        var2 : 4
        unused : 3
    };
    Stuff stuff;
    Assuming the sytax is correct (not sure about that ), could I say "stuff.var = true;"? And then, could I manipulate var2 as an int?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    The syntax is incorrect, but yeah, you are on the right track. You still have to give your struct members datatypes! Also, You don't need to put in the "unused" 3 bit wide variable (unless you are planning on using it, , which i'm doubting from the name, or for some reason just want those 3 bits to be of a certain value). Your structure will automatically be padded, just like any other case.

    Code:
    struct Stuff
    {
        int var : 1,
             var2 : 4,
             unused : 3;
    };
    Also, it appears as though you want var to be used as a bool. You can declare it as a bool then!

    Code:
    struct Stuff
    {
        bool var : 1;
        int var2 : 4;
    };
    Is all you need.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Oh, I get it so you still need the type to tell it how to handle the variables, right?

    Oh, and by the way, the variable size is in bits, right (i.e. "bitfields")?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structure Bitfields
    By samGwilliam in forum C Programming
    Replies: 6
    Last Post: 09-04-2009, 09:45 PM
  2. Bitfield
    By $l4xklynx in forum C Programming
    Replies: 26
    Last Post: 12-25-2008, 08:52 AM
  3. Bitfields, Bit/Little Endian
    By azerej in forum C Programming
    Replies: 0
    Last Post: 05-26-2008, 02:01 AM
  4. Bitfields, Bit/Little Endian
    By plan7 in forum C Programming
    Replies: 17
    Last Post: 11-08-2007, 01:48 AM
  5. portable bitfields
    By Yarin in forum C++ Programming
    Replies: 2
    Last Post: 08-14-2007, 01:39 PM