Sign in to follow this  

Populating a bit-field

This topic is 3590 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! I want to store this class bit field into a file, and read it up again as an int. How do i get the address of the bitfield, I mean it has no name... Can i name it somehow? I dont really want to put it inside another struct as it is used all ower the project already. Further more I'm using VS2005, how can i somehow control that the bitfield really become packed into an int? Thanks! /TC

typedef unsigned int U32;

class CMicroTile : public CBasicMicroTile
{
public:
	CMicroTile();	
	// Public Members
									/**< 0-31	Bits total: */
	U32 m_nNode_1 : 4;				/**< 0-3	Closest size 1 macro node --> for small units	*/
	U32 m_nNode_3 : 4;				/**< 4-7	Closest size 3 macro node --> for medium sized units*/
	U32 m_nNode_5 : 4;				/**< 8-11	Closest size 5 macro node --> for large units */
	U32 m_bitPassable : 3;			/**< 12-14	Distance to unpassable ground 0 = unpassable, 7 = 7 or longer */
	U32 m_bitOccupied : 1;			/**< 15		Temporary occupied bit used in pathing and collision */
	U32 m_bitCovered : 3;			/**< 16-18	Bit deciding the amount of LOS the tile blocks 7 == Completly blocked*/
	U32 m_nObstructionCount : 2;	/**< 19-20	Obstruction count used to build passability */
	U32 m_nTerrainObstruction : 1;	/**< 21		Obstructive terrain 1 = yes, 0 = no */
	U32 m_bitMovement : 2;			/**< 22-23	Speed penalty for movement 0-25%-50%-75% */
	U32 : 8;						/**< 24-31	Padding (unnamed fields are used for padding */	
};

</code>

Share this post


Link to post
Share on other sites
Stick it in a struct and union it with a single u32. The union and the struct can both be unnamed, if you don't want to do too much typing every time you need to use it.


class blah
{
public;
union
{
struct
{
uint someBits :3;
uint someMoreBits : 2;
uint aNybble : 4;
uint etc : 25;
};
uint word;
};
};

int main(void)
{
blah bleh;

// zero the bitfield;
bleh.word = 0;
// or use memset if you like
memset(&bleh.word, 0, sizeof(bleh.word));

// set some bit values
bleh.someBits = 2;
bleh.aNybble = 17;
}


Share this post


Link to post
Share on other sites
I'm a little suspicious of bitfields in general. AFAIK those unnamed union/struct tricks aren't fully portable, either. (And what's with trying to put the value 17 into a 4-bit field? ;) )

I'm a little bored right now, so (not tested!)


class bitset_packer {
int begin, size;
public:
bitset_packer(int size): begin(0), size(size) {}
bitset_packer(int begin, int size): begin(begin), size(size) {}
bitset_packer(const bitset_packer& previous, int size): begin(previous.end()), size(size) {}
template <int N>
void set(std::bitset<N>& bs, int value) {
assert (N >= begin + size);
int field = 1 << size;
bs &= (field - 1) << begin;
bs |= (value % field) << begin;
}
template <int N>
int get(const std::bitset<N>& bs) {
assert (N >= begin + size);
int field = ((1 << size) - 1) << begin;
return (bs & field) >> begin;
}
int end() { return begin + size; }
};


static const bitset_packer
nNode_1(4), nNode_3(nNode_1, 4), nNode_5(nNode_3, 4),
bitPassable(nNode_5, 3), bitOccupied(bitPassable, 1), bitCovered(bitOccupied, 3),
nObstructionCount(bitCovered, 2), nTerrainObstruction(nObstructionCount, 1),
bitMovement(nTerrainObstruction, 2);
// If this doesn't work as a compile time calculation, then I guess you have to
// just pick your bitset size and rely on the asserts to be sure you picked right...
static const int TILE_SIZE = bitMovement.end();

class CMicroTile : public CBasicMicroTile
{
// Padding should happen automatically...
public:
CMicroTile();
std::bitset<TILE_SIZE> tile;
};

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
(And what's with trying to put the value 17 into a 4-bit field? ;) )


it's a very small 17 being packed into a very large 4 bits. [grin]

Share this post


Link to post
Share on other sites

This topic is 3590 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this