Thread: What does the : indicate in a struct?

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    31

    What does the : indicate in a struct?

    Code:
    typedef struct
    {
    	uint8_t				bB0:1;	
    	uint8_t				bB1:1;	
    	uint8_t				bB2:1;	
    	uint8_t				bB3:1;	
    	uint8_t				bB4:1;	
    	uint8_t				bB5:1;	
    	uint8_t				bB6:1;	
    	uint8_t				bB7:1;
    }testStruct
    What does the : indicates in the above struct? Does it mean 1 bit for each of them and total up to 8 bits?

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Look up Bit Fields

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Edelweiss View Post
    Code:
    typedef struct
    {
    	uint8_t				bB0:1;	
    	uint8_t				bB1:1;	
    	uint8_t				bB2:1;	
    	uint8_t				bB3:1;	
    	uint8_t				bB4:1;	
    	uint8_t				bB5:1;	
    	uint8_t				bB6:1;	
    	uint8_t				bB7:1;
    }testStruct
    What does the : indicates in the above struct? Does it mean 1 bit for each of them and total up to 8 bits?
    In addition to Fordy's good advice...

    Yes, that's what it means. As 1 bit veriables each of the elements has only 2 states... 0 or 1, usually used as on and off or true and false.
    An alternate form of bitfields can look like this...
    Code:
    typedef struct
    {
       uint8_t  bB0:1,	
                bB1:1,
                bB2:1,	
                bB3:1,	
                bB4:1,	
                bB5:1,	
                bB6:1,	
                bB7:1;
    }testStruct
    But they amount to the same thing...

    You should also note that not all compilers support uint8_t as a bitfield. The more common root is unsigned integer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-30-2011, 03:24 PM
  2. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  3. 2 problems: Struct array and pointer + struct
    By v1n1c1u5 in forum C Programming
    Replies: 0
    Last Post: 12-13-2009, 05:38 PM
  4. struct holding data inside a linked list struct
    By icestorm in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 12:49 PM
  5. Replies: 1
    Last Post: 05-05-2004, 06:58 AM