hello..

this is srini... is any one clear me the concept of BIT FIELDS........and what is necessity to use?????

explain the below program..........i hope i can get a good reply from the great members

// BIT FILED //

#include<iostream.h>
#include<conio.h>
struct word
{
unsigned w0:1,w1:1,w2:1,w3:1,w4:1,w5:1,w6:1,w7:1,w8:1,w9:1,w10:1,w11:1,w12:1,w13:1,w14:1,w15:1,w16:1,w17:1,w18:1,w19:1,w20:1,w21:1,w22:1,w23:1,w24:1,w25:1,w26:1,w27:1,w28:1,w29:1,w30:1,w31:1;
};
union set
{
word m;
unsigned u;
};
void main()
{
set x,y;
x.u=0x0f100f10;
y.u=0x01a1a0a1;
x.u=x.u|y.u;
cout<<"element 9="<<((x.m.w9)?"true":"false")<<endl;
}

OUTPUT

ELEMENT 9 =TRUE

Edited 6 Years Ago by WaltP: Added CODE tags -- with all the help about them, how could you miss using them????

Bit Fields give you direct access to the bits contained in data. I'm currently using a bit field as active/inactive flags for rules variations for a game. In general they are used as a compressed method of storing several true/false (bool) values.

The bit field itself is the struct named "word". The data type that it creates is then included as part of a union with an unsigned integer which creates (in general) a set of 32-bits. You can then manipulate the bits by referencing them individually with their assigned names ('w0','w1', etc...) or you can modify them as an entire set by modifying the variable called 'u' in the union.

Here is my bit field:

#include <climits>

#define RuleActive 1
#define RuleInactive !RuleActive

union bitFlags {
  unsigned short flagGroup;  //provide a 16-bit backbone (on 32-bit systems)
  struct {    //anonymous struct, the bit field itself
    unsigned multiplierRule	:  1;	//override for bonus applied by multiples-of-a-kind rule,
					//"on" causes values to be 2x,3x,4x three-of-a-kind value resp.
    unsigned straightRule	:  1;	//"on" activates bonus for a straight in a single roll
    unsigned threePairRule	:  1;	//"on" activates bonus for three pairs in a single roll
    unsigned hardWinRule	:  1;	//"on" activates the hard win rule-set
    unsigned unusedBits	:  (sizeof(short)*CHAR_BIT)-4;
  };	//close the bitfield structure
};	//close the union

Each "flag" has an associated "toggle" function to turn it on/off. Then in the game's logic it checks the status of the appropriate bit to determine how it should score various combinations. This configuration allows me to store up to 16 boolean values in 2 bytes instead of 16.

[edit]
And please use code tags. You had them in before, why did you take them out?

Edited 6 Years Ago by Fbody: corrected code formatting, it got mangled

> unsigned unusedBits : (sizeof(short)*CHAR_BIT)-4;
What is this for?

What about specifying a width of zero?

A bit-field declaration with no declarator, but only a colon and a width, indicates an
unnamed bit-field. As a special case, a bit-field structure member with a width of 0
indicates that no further bit-field is to be packed into the unit in which the previous bit-field, if any, was placed

It's a lot easier to maintain than remembering how many bits to subtract.

> unsigned unusedBits : (sizeof(short)*CHAR_BIT)-4;
What is this for?

What about specifying a width of zero?

It's a lot easier to maintain than remembering how many bits to subtract.

I've been reading Schildt's book and he says essentially the same thing so that's what I thought too. What I posted is my implementation based on an example in an e-mail conversation with my instructor (the game is a "capstone" design project for the class). His example actually hard-coded the "unused" number and his comment made it sound like it needed to be. I thought, for portability, it would be better to do it how I did. The reason is that as I understand it (I have seen several different versions of what it is), a short isn't necessarily specified as 16-bits, it's specified relative to the size of a char, which just means at least 8-bits.

There are some things my instructor does that I don't necessarily agree with, this being one of them. I just didn't feel like questioning him on it at the time, and it worked, so I ran with it. I will take a closer look at it based on your post though.

[edit]
I quick modified the union to this:

union bitFlags {
  unsigned short flagGroup;  //provide a 16-bit backbone (on 32-bit systems)
  struct {
    unsigned multiplierRule	:  1;	//override for bonus applied by multiples-of-a-kind rule,
					//"on" causes values to be 2x,3x,4x three-of-a-kind value resp.
    unsigned straightRule	:  1;	//"on" activates bonus for a straight in a single roll
    unsigned threePairRule	:  1;	//"on" activates bonus for three pairs in a single roll
    unsigned hardWinRule	:  1;	//"on" activates the hard win rule-set
    //unsigned unusedBits		:	(sizeof(short)*CHAR_BIT)-4;
  };	//close the bitfield structure
};	//close the union

and it seems to work. The test program i've been using compiled and ran without an issue. Thanks, Salem!

Edited 6 Years Ago by Fbody: added test results

This article has been dead for over six months. Start a new discussion instead.