Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 7, 2010, 04:42 PM   #1
North Bronson
macrumors 6502
 
Join Date: Oct 2007
Location: Los Angeles
Bit Fields

Hello. If anyone could help me understand how to use these bit-fields, that would be great.

Both of my C texts tell me that I can instantiate a struct like this:

Code:
#include <stdio.h>

int main (int argc, char *argv[])
{
    struct intStruct
    {
        unsigned int value0 : 1;
        unsigned int value1 : 1;
        unsigned int value2 : 1;
        unsigned int value3 : 1;
        unsigned int value4 : 1;
        unsigned int value5 : 1;
        unsigned int value6 : 1;
        unsigned int value7 : 1;
    };
    
    printf("%lu\n", sizeof(struct intStruct));
    
    return 0;	
}
The console tells me that this struct is four bytes long. Shouldn't it be only one byte?

If I try this:

Code:
#include <stdio.h>

int main (int argc, char *argv[])
{
    struct charStruct
    {
        unsigned char value0 : 1;
        unsigned char value1 : 1;
        unsigned char value2 : 1;
        unsigned char value3 : 1;
        unsigned char value4 : 1;
        unsigned char value5 : 1;
        unsigned char value6 : 1;
        unsigned char value7 : 1;
    };
    
    printf("%lu\n", sizeof(struct charStruct));
    
    return 0;	
}
The console tells me that the struct is only one byte, but both of my C texts tell me not to use chars for bit-fields.

If anyone can explain what's going on, that would be great.
North Bronson is offline   0 Reply With Quote
Old Nov 7, 2010, 04:56 PM   #2
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Why would the size of 8 ints be 1 byte? That would imply each int was only 1 bit so could only represent 2 values. Ints can obviously represent more values than that...
robbieduncan is offline   0 Reply With Quote
Old Nov 7, 2010, 05:03 PM   #3
subsonix
macrumors 68030
 
Join Date: Feb 2008
The compiler might add padding to make the program faster and more efficient. You can force it to be packed with gcc, which might lead to a less efficient program but anyway. Try this.

Code:
#include <stdio.h>

int main (int argc, char *argv[])
{
    struct intStruct
    {
        unsigned int value0 : 1;
        unsigned int value1 : 1;
        unsigned int value2 : 1;
        unsigned int value3 : 1;
        unsigned int value4 : 1;
        unsigned int value5 : 1;
        unsigned int value6 : 1;
        unsigned int value7 : 1;
    }__attribute__((packed));

    printf("%lu\n", sizeof(struct intStruct));

    return 0;
}
subsonix is offline   0 Reply With Quote
Old Nov 7, 2010, 05:04 PM   #4
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
To get an almost authoritative answer, google for "N1256.pdf" which will find the last free draft of the C99 Standard. From that document:

"A bit-field shall have a type that is a qualified or unqualified version of _Bool, signed int, unsigned int, or some other implementation-defined type."

In other words, while the gcc compiler may accept bitfields of type "unsigned char", other compilers might not. Bit fields of type "unsigned int" are portable.

On the other hand, there is no danger in using bit fields of type char or unsigned char: If the compiler accepts it then it works; if it doesn't work the compiler will tell you, so you can leave it to whoever tries to port the code to make the change (a comment would be nice so that a programmer having to fix a compiler error knows what is going on).
gnasher729 is offline   0 Reply With Quote
Old Nov 7, 2010, 05:50 PM   #5
North Bronson
Thread Starter
macrumors 6502
 
Join Date: Oct 2007
Location: Los Angeles
Quote:
Originally Posted by robbieduncan View Post
Why would the size of 8 ints be 1 byte? That would imply each int was only 1 bit so could only represent 2 values. Ints can obviously represent more values than that...
I thought that by using bit-fields I could force the compiler to build each int into only one-bit. Isn't that the what bit-fields are supposed to be able to do?
North Bronson is offline   0 Reply With Quote
Old Nov 7, 2010, 05:52 PM   #6
North Bronson
Thread Starter
macrumors 6502
 
Join Date: Oct 2007
Location: Los Angeles
Quote:
Originally Posted by subsonix View Post
The compiler might add padding to make the program faster and more efficient. You can force it to be packed with gcc, which might lead to a less efficient program but anyway. Try this.

Code:
#include <stdio.h>

int main (int argc, char *argv[])
{
    struct intStruct
    {
        unsigned int value0 : 1;
        unsigned int value1 : 1;
        unsigned int value2 : 1;
        unsigned int value3 : 1;
        unsigned int value4 : 1;
        unsigned int value5 : 1;
        unsigned int value6 : 1;
        unsigned int value7 : 1;
    }__attribute__((packed));

    printf("%lu\n", sizeof(struct intStruct));

    return 0;
}
Interesting. That code that you added works and the struct is only one byte now.
North Bronson is offline   0 Reply With Quote
Old Nov 7, 2010, 05:54 PM   #7
North Bronson
Thread Starter
macrumors 6502
 
Join Date: Oct 2007
Location: Los Angeles
Quote:
Originally Posted by gnasher729 View Post
To get an almost authoritative answer, google for "N1256.pdf" which will find the last free draft of the C99 Standard. From that document:

"A bit-field shall have a type that is a qualified or unqualified version of _Bool, signed int, unsigned int, or some other implementation-defined type."

In other words, while the gcc compiler may accept bitfields of type "unsigned char", other compilers might not. Bit fields of type "unsigned int" are portable.

On the other hand, there is no danger in using bit fields of type char or unsigned char: If the compiler accepts it then it works; if it doesn't work the compiler will tell you, so you can leave it to whoever tries to port the code to make the change (a comment would be nice so that a programmer having to fix a compiler error knows what is going on).
Good to know. I'm building this on Xcode for Snow Leopard (with GCC). I should probably check what LLVM would do with that same struct.
North Bronson is offline   0 Reply With Quote
Old Nov 7, 2010, 06:11 PM   #8
subsonix
macrumors 68030
 
Join Date: Feb 2008
Quote:
Originally Posted by North Bronson View Post
I thought that by using bit-fields I could force the compiler to build each int into only one-bit. Isn't that the what bit-fields are supposed to be able to do?
I think the main idea it to provide an easier to use interface then dealing with bitwize operations. It also gives you the added benefit of being able to give meaningful names to the bit positions. You could just as well have used an unsigned char directly if you wanted to.

Also, if something is 1 byte or 4 is less of a concern today and if the compiler thinks it's more efficient to align data in 32 bit boundries it will do so by adding some padding. But as you saw, it's possible to suggest that it should not be done if it's necessary for some reason.

Last edited by subsonix; Nov 7, 2010 at 06:24 PM.
subsonix is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
yellow highlighting form fields paulold Mac OS X 10.7 Lion 0 Jul 23, 2011 08:58 AM
Activity Monitor bytes vs. bits eah2119 OS X 0 Jan 4, 2011 07:01 PM
Script or something for copying Year field to Notes or BPM field in iTunes? devburke Mac Applications and Mac App Store 2 Jun 7, 2009 01:47 PM
Giant oil field to raise Saudi output - largest oil field development yet edesignuk Politics, Religion, Social Issues 2 Jul 11, 2008 05:01 AM
scrolling text field and text fields? liptonlover Mac Programming 0 Jun 25, 2008 08:44 AM


All times are GMT -5. The time now is 10:48 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC