# Thread: bit field

1. ## 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. 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);

}```

3. Code:
`unsigned int type    : 6;`
What exactly is that doing? Setting the number of accessible bits...?

4. Code:
```struct test
{
unsigned int type : 6;
};```
how much memory will that use?

5. > how much memory will that use?
sizeof( struct test )

6. 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.

7. 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!

Popular pages Recent additions