In C ++, it is possible to set a certain number of elements of the structure in memory bits. For example, if you want to create a data structure, size corresponding register in any device. The type of element (it is called a bit field) such a structure may be an integer (most frequently of type unsigned) or enumerable (enum).
unsigned short first : 2;
unsigned short second: 2;
unsigned short third : 4;
We have determined the structure of, wherein the variables specified number will occupy bits. 2 + 2 + 4 It gives 8 bit (We lined up to size bytes). If you finish in this structure still unsigned short fifth : 5; – It will already be involved 2 bytes. The second byte would of course junk 8 – 5 = 3 bits, to be unclaimed.
Unlike associations (union) size of bit fields varies, whichever, how many bits the programmer ordered. If ordered 7 bit (say the two variables on 3 bits, and one – 1 bit), then C ++ will take one byte (8 bit) under these three variables. If the programmer will order 11 bit, then C ++ will take two bytes (16 bit). And in the second byte will be used only 5 bit, and the rest are likely to be, as useless tail. Therefore, in the description of bit fields should be considered a “alignment” to bytes. That is. distribute it as variables, so that each bit has been claimed. For alignment of memory you can use unnamed bit fields.
Here is another short example, wherein the bit fields are reserved for the date and time for the demonstration of the technology.
using namespace std;
struct DateTime //Опишем структуру с битовыми полями
unsigned short Day : 5; //5 бит для дня
unsigned short Month : 4; //4 для месяца
unsigned short Year : 7; //7 для года от 0 до 99
unsigned short Hour : 5; //5 бит для 24-х часов
unsigned short Minute : 6;//6 для минут
unsigned short Second : 6;//6 для секунд
DateTime d; //объявляем переменную этого типа с битовыми полями
int i; //И еще одну, в которую будет поступать ввод данных
cout << "Input day (1-31):" << '\t'; cin >> i; d.Day = i;
cout << "Input month (1-12):" << '\t'; cin >> i; d.Month = i;
cout << "Input Year (00-99) :" << '\t'; cin >> i; d.Year = i;
cout << endl << "Input Hour (0-24):" << '\t'; cin >> i; d.Hour = i;
cout << "Input Minute (0-60):" << '\t'; cin >> i; d.Minute = i;
cout << "Input Seconds (0-60):" << '\t'; cin >> i; d.Second = i;
//И выведем их с показателем размера в памяти
cout << endl << "Date is: " << d.Day << "." << d.Month << ".20" << d.Year << " ";
cout << d.Hour << ":" << d.Minute << ":" << d.Second << endl;
cout << sizeof(d) << endl;
As you can see, bit fields store date and time. It takes this structure 6 bytes, although it is enough for five. And then there are reasons: the compiler may align withdrawn memory to an even number of bytes. For example, if we had booked 18 bit, the compiler will take us not 3 bytes, and 4, considering that the CPU likes to work with bytes, and not with bits. At least the memory or processor registers its bits prefers not, namely bytes. According to his bit: x32 feed 4 bytes, x64 has 8 bytes. Even though these bytes work is only one of them, the rest will still catch up.
Small outcome: Bit fields in structures commonly used in low-level programming, when the work is with values, able to take no bytes, and the individual bits (since, that small values).