Thread: 2 questions about bit fields

  1. #1
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211

    2 questions about bit fields

    I am just contemplating whether or not I should use bit fields on my struct's members to minimize the size of the struct.

    in my book (Teach Yourself C Third Edition) it says that the type of a bit field is either int or unsigned, and that if its a signed int that the high order bit will be treated as the sign bit if possible.

    now int and unsigned are both 32-bit ints. does this mean that I can only use bit fields on members declared as 32-bit types?

    also, it mentions that the smallest addressable unit of memory is a byte, thus meaning, you cannot obtain the address of a bit field. how about obtaining the address of the struct that the bit fields are members of? is that possible?

    as I said this is all an attempt to minimize the size of my code. since the bit fields I'd use would be small, if this is possible, it will substantially reduce the size of the struct.

    any help here is greatly appreciated. thank you in advance!
    Last edited by Bleech; 11-13-2006 at 02:44 AM.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > as I said this is all an attempt to minimize the size of my code. since the bit fields I'd use would be small, if this is possible, it will substantially reduce the size of the struct.
    This would be the wrong reason to use a bit field. Reducing the size of a struct can be helped along if you arrange fields carefully, where types with the most restrictive alignment requirements (double) are placed before those with the least restrictive (char). But in general little can be done.

    > now int and unsigned are both 32-bit quantities.
    This may not be true in some places.

    > does this mean that I can only use bit fields on members declared as 32-bit types?
    No.

    > how about obtaining the address of the struct that the bit fields are members of? is that possible?
    Yes.
    Last edited by whiteflags; 11-13-2006 at 02:53 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I am just contemplating whether or not I should use bit fields on my struct's members to minimize the size of the struct.
    Unless you have many 1000's of instances AND you're on a machine with not much memory (not your typical desktop), then probably not.

    Given
    unsigned int small : 2;
    As a typical bit field member, the compiler has to generate something like
    ( x >> number ) & 0x03
    every time you want to access that field.
    Which is going to increase the code space and decrease the performance if you do a lot of it.
    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.

  4. #4
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    ok, thanks guys, I won't use them then (I'll just use short ints).

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Your operating system probably pages memory to disk anyway, so it shouldn't matter. But it could be useful for combatting memory bottlenecks by (possibly, through a careful implementation) using the pipeline and cache more efficiently than usual. Using bitfields this way should only be considered if your monster program incurs some huge performance drain on the system and there is not much software optimization leeway remaining.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The preformance of bitfields can be vastly different between compilers. IIRC, MSVC++ 6 has a horribly slow implementation when compared to GCC or even Borland's old compilers. If you're up to it, search on the topic. There was a thread some time (long time) back where a bunch of people ended up testing said preformance.


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

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by sl34k
    I am just contemplating whether or not I should use bit fields on my struct's members to minimize the size of the struct.
    It might achieve that, but that benefit is offset by other costs .... see previous posts, and later in this one.
    Quote Originally Posted by sl34k
    in my book (Teach Yourself C Third Edition) it says that the type of a bit field is either int or unsigned,
    A bit field is of signed or unsigned integral type.
    Quote Originally Posted by sl34k
    and that if its a signed int that the high order bit will be treated as the sign bit if possible.
    I don't have my copy of the standard handy, but I doubt it. This statement might be true with some particular implementations.
    Quote Originally Posted by sl34k
    now int and unsigned are both 32-bit ints.
    The C standard does not require int or unsigned types to be 32 bits. It is possible to find compilers where they are 16 bits.
    Quote Originally Posted by sl34k
    does this mean that I can only use bit fields on members declared as 32-bit types?
    Unclear what you mean, but whatever you mean the answer is "no".
    Quote Originally Posted by sl34k
    also, it mentions that the smallest addressable unit of memory is a byte, thus meaning, you cannot obtain the address of a bit field. how about obtaining the address of the struct that the bit fields are members of? is that possible?
    The size of any variable (basic types, a struct, or a type that contains bitfields) is at least one byte, that variable is addressable.
    Quote Originally Posted by sl34k
    as I said this is all an attempt to minimize the size of my code. since the bit fields I'd use would be small, if this is possible, it will substantially reduce the size of the struct.
    Depends on what you mean by "code size". In practice, the machine has to do more work (execute more instructions) to get or set the value of individual bits than it does to manipulate bytes or other addressable types. That translates into more machine instructions, and therefore into bigger compiled object or executable files that (often) will run more slowly. The actual source code to set/get the value of a particular bitfield will not be substantially different in size from source code that works with distinct addressable variables (int, char, etc) instead of with bit fields.

    Bit fields are most useful in circumstances where you need to conserve memory for data used by the program, where that memory is at a premium, and you are prepared to accept the cost of a bigger executable file that may run more slowly. With modern computing equipment (eg with many megabytes of RAM, on 32 bit operating systems) there are few benefits to using bit fields to justify the effort or cost. On some older equipment (eg with a few bytes of memory for data storage) it may be warranted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-28-2009, 01:28 PM
  2. Optimizing question(s)...
    By yaya in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2009, 03:05 AM
  3. advanced bit fields
    By gooddevil in forum C Programming
    Replies: 3
    Last Post: 05-11-2004, 01:53 PM
  4. Bitwise Operators....
    By o0obruceleeo0o in forum C++ Programming
    Replies: 21
    Last Post: 04-09-2003, 06:45 AM