struct FileAttributes
{
unsigned int ReadOnly: 1;
unsigned int Hidden: 1;
};
Here, each of these two fields will occupy 1 bit in memory. It is specified by : 1
expression after the variable names. Base type of bit field could be any integral type (8-bit int to 64-bit int). Using unsigned
type is recommended, otherwise surprises may come.
If more bits are required, replace "1" with number of bits required. For example:
struct Date
{
unsigned int Year : 13; // 2^13 = 8192, enough for "year" representation for long time
unsigned int Month: 4; // 2^4 = 16, enough to represent 1-12 month values.
unsigned int Day: 5; // 32
};
The whole structure is using just 22 bits, and with normal compiler settings, sizeof
this structure would be 4 bytes.
Usage is pretty simple. Just declare the variable, and use it like ordinary structure.
Date d;
d.Year = 2016;
d.Month = 7;
d.Day = 22;
std::cout << "Year:" << d.Year << std::endl <<
"Month:" << d.Month << std::endl <<
"Day:" << d.Day << std::endl;