Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simplest, most efficient way to store bit fields?

Status
Not open for further replies.

thesleepylizard

Programmer
Mar 18, 2002
41
AU
How good is this C++ bit-fielding-code?

1) Firstly, on generic bit fielding code.

I'm writing some bit fielding code which needs to be as fast as it gets, as it gets called zillions of times a second on a server responding to requests in real time.

I'm reluctant to use any of the standard, generic bitfielding routines in case they are more designed for flexibility instead of performance, and performance is top priority in this situation.

So, I've written the following routines, which basically work:

Code:
char* PrepareBitField (int Length) {

	//Quickly convert Length to how many BYTES we need for the bit field. If the bits is divisible by 8, then we right-shift the number thrice. If it's not divisible by 8, then we rightshift it thrice and add 1.
	if (Length % 8 == 0) {
		Length>>=3;
	} else {
		Length>>=3;
		Length++;
	}

	//Allocate the appropriate number of bytes
	char* rv = (char*)malloc(Length); 

	//And clear them out
	memset (rv, (char)0, Length); 
	return rv;
}
void StoreBoolInBitField (char* BitField, int BitIndex, char Value) {

	if (Value) {//Don't bother if it's False.

		//Move the "1" *bit* to the appropriate location in the *byte*, which is soon to be "OR'ed" into the appropriate *byte* in the *buffer*.
		Value <<= (BitIndex % 8); 

		//Move the byte *pointer* the the appropriate *byte* in the *buffer*, which is soon to have the appropriate *bit* &quot;OR'ed&quot; into it.
		BitField += (BitIndex >> 3); 
		
		//&quot;OR&quot; the appropriate *bit* into the appropriate *byte*, and our task is completed.
		*BitField |= Value; 
	}
}
bool GetBitInBitField (char* BitField, int BitIndex) {

	//Create the appropriate bit *mask* to &quot;AND&quot; to the appropriate *byte*, which will return the appropriate bit.
	char Mask = 1;
	Mask <<= (BitIndex % 8); 

	//Move the byte *pointer* to the appropriate *byte* in the *buffer*.
	BitField += (BitIndex >> 3); 

	//Then &quot;AND&quot; the *byte* to the *mask*, returning the bit we're after.
	return (*BitField & Mask == 0)?0:1;
}

Any thoughts on this code? Is there a better, faster way to allow generic bit fielding?

(Sorry it's a bit lengthy btw)



2) Secondly, on bit fielding where the number of bits is already known. For example, I might have the following variables:

Code:
bool Boolean1 = 0;
bool Boolean2 = 1;
bool Boolean3 = 1;
bool Boolean4 = 1;
bool Boolean5 = 1;
bool Boolean6 = 0;
char Mask = NULL;

I want to store these 6 booleans into the Mask variable, but as I already known exactly how many there are going to be and what they are, I should be able to get much better performance than I could through using the generic bit fielding routines above.

As a super-lazy, I-want-to-move-onto-other-things fix, I wrote it as follows:

Code:
Mask = (Boolean1?0x01) | (Boolean2?0x02) | (Boolean3?0x04) | (Boolean4?0x08) | (Boolean5?0x10 | (Boolean6?0x20)

Of course, this is unreadable, and difficult to modify.

Is there an alternative way to compress a known number of booleans into a bit field like this, which is as fast as it can possibly get? (which for a known number of booleans, the generic functions would not be...?)

Thanks in advance for your responses,
-sleepy
 
Use structures with bitfields:

typedef struct
{
int bit0:1;
int bit1:1;
int bit2:1;
int bit3:1;
int bit4:1;
int bit5:1;
int bit6:1;
int bit7:1;

...

} your_bitfield;

It is more elegant and better readable.

The speed of both approaches I think is similar, if you want more speed, convert your code to assembler, but gain of the speed will be microscopic in comparison to server responces time through the network.
 
beware of endianism when using bitfields in a struct! Bit me in the butt a few times :)


Matt
 
By the way, you can use STL Ion Filipski
1c.bmp


filipski@excite.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top