Thread: bit field

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    26

    bit field

    Code:
    struct x_mttype   {   struct   {   unsigned int type    : 6;
                                       unsigned int locale  : 1;
                                       unsigned int obs     : 1;
                                       unsigned int palette : 6;
                                       unsigned int         : 2;
                                   } var;
                          char tchar;
                      };
    
    x_mttype s_maptemp[19][79];
    How many permutations can I use with 6 bits?

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    If your question is how many variations you can have with 6 bits (bit - 0 or 1) then answer is 2^6 = 64;
    000000 (0)
    000001 (1)
    ....
    111111 (63)
    Total number of possible variations is 64!

    check this
    Code:
    #include <stdio.h>
    
    struct test
    	{
    		unsigned int type : 6;
    	};
    	
    int main(void)
    {
    	struct test ts;
    	ts.type = 63;
    	printf("%d",ts.type);
    	ts.type++;
    	printf("\n%d",ts.type);
    	
    }
    Last edited by Micko; 03-22-2005 at 02:22 AM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Code:
    unsigned int type    : 6;
    What exactly is that doing? Setting the number of accessible bits...?

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    26
    Code:
    struct test
    	{
    		unsigned int type : 6;
    	};
    how much memory will that use?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > how much memory will that use?
    sizeof( struct test )
    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.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Quote Originally Posted by jverkoey
    Code:
    unsigned int type    : 6;
    What exactly is that doing? Setting the number of accessible bits...?
    Look up bit fields You can allocate memory in a struct in interesting ways with bit fields.

    So if you had:
    Code:
    struct bitMapping
    {
      unsigned int uint_bit128 : 7;
      unsigned int uint_bit64 : 6;
      unsigned int uint_bit32 : 5;
      unsigned int uint_bit16 : 4;
      unsigned int uint_bit8 : 3;
      unsigned int uint_bit4 : 2;
      unsigned int uint_bit2 : 1;
    } bitMap_One;
    So, in this fashion, the first unsigned int named uint_bit128 can allocate a maximum of 256, since we're allocating 7 bits for this variable. The second unsigned int named uint_bit64 can allocate a maximum of 128, since we're allocating 6 bits for the variable, that sort of thing. It isn't how many bits you can access necessarily, but how many bits you're allocating for that variable. Effectively making file sizes much smaller.

    Bit fields are just a way to modify the maximum values for variables in such a way to maximize efficiency. Many programs can be optimized with the use of bit fields.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  7. #7
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Here's a little more about it. I found it on somewhere on the Internet:
    A bit field is a structure member that contains a specified number of bits. You can declare a bit field to contain one bit, two bits, or whatever number of bits are required to hold the data stored in the field. What advantage does this provide?

    Suppose that you're programming an employee database program that keeps records on your company's employees. Many of the items of information that the database stores are of the yes/no variety, such as "Is the employee enrolled in the dental plan?" or "Did the employee graduate from college?" Each piece of yes/no information can be stored in a single bit, with 1 representing yes and 0 representing no.

    Using C's standard data types, the smallest type you could use in a structure is a type char. You could indeed use a type char structure member to hold yes/no data, but seven of the char's eight bits would be wasted space. By using bit fields, you could store eight yes/no values in a single char.

    Bit fields aren't limited to yes/no values. Continuing with this database example, imagine that your firm has three different health insurance plans. Your database needs to store data about the plan in which each employee is enrolled (if any). You could use 0 to represent no health insurance and use the values 1, 2, and 3 to represent the three plans. A bit field containing two bits is sufficient, because two binary bits can represent values of 0 through 3. Likewise, a bit field containing three bits could hold values in the range 0 through 7, four bits could hold values in the range 0 through 15, and so on.

    Bit fields are named and accessed like regular structure members. All bit fields have type unsigned int, and you specify the size of the field (in bits) by following the member name with a colon and the number of bits. To define a structure with a one-bit member named dental, another one-bit member named college, and a two-bit member named health, you would write the following:
    Cheers!
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for a little assistance with item class design...
    By Raigne in forum Game Programming
    Replies: 5
    Last Post: 10-22-2008, 08:55 AM
  2. bit level permutation function
    By zxcv in forum C Programming
    Replies: 2
    Last Post: 07-27-2008, 01:26 PM
  3. Porting from 32 bit machine to 64 bit machine!
    By anoopks in forum C Programming
    Replies: 10
    Last Post: 02-25-2005, 08:02 PM
  4. Replies: 7
    Last Post: 12-10-2004, 08:18 AM
  5. BIT field confusing
    By C-Dumbie in forum C Programming
    Replies: 4
    Last Post: 09-14-2002, 02:33 AM