thesleepylizard
Programmer
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:
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:
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:
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
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* "OR'ed" into it.
BitField += (BitIndex >> 3);
//"OR" 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 "AND" 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 "AND" 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