# Change storage size of an STL bitset?

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

## Recommended Posts

Is it possible to create a bitset<16> that only uses 2 bytes instead of 4? From what I gather from the STL documentation I have, sizeof(bitset<N>) is undefined (i.e. defined by the implementation and not the standard), so I'm guessing the answer is no, but if that is the case, I'd like suggestions as to how to implement the following: I need to read records in a database file that was written by an old Pascal program. The file format is fixed the way it is and cannot reasonably be changed. Some of the fields are 16-bit bitfields. I'm currently reading them as unsigned shorts and performing bit operator manipulations on them manually to test various flags, but would vastly prefer using the STL where possible. (This forum is constantly saying not to reinvent the wheel.) The problem is if I create a structure like the following:
struct fileRecord {
unsigned short value1;
char value2[6];
double value3;
bitset<16> value4;
unsigned long value5;
};


##### Share on other sites
Well, the problem is that you need to align that last long (value5) on a 2-byte boundary instead of 4. You can change your default project alignment properties, or you can look into the #pragma pack() directive.

Also if you want named fields for your bitset, you can use a bit field.

##### Share on other sites
Quote:
 Original post by ajas95Well, the problem is that you need to align that last long (value5) on a 2-byte boundary instead of 4. You can change your default project alignment properties, or you can look into the #pragma pack() directive.

If you're talking about what I think you are, I already changed the compiler options because some of the structure data is single chars and I was having trouble getting the structures to be the right size without setting "struct member alignment" to 1 byte.

My issue is even without a structure involved, bitsets allocate memory in blocks of 4 bytes. I was wondering if there was any way to change this without implementing my own custom bitset type or just using bit manipulation on a short. Sorry if I was unclear.

Quote:
 Also if you want named fields for your bitset, you can use a bit field.

I was just using enums for the flag names. Could you point me to information on "bit fields"? I was of the impression the only standard types that meet my needs were vector<bool> and bitset<N>, and since bitsets have the sorts of member functions that fit my needs perfectly, I'd prefer those.