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).
Syntax-bit field in the structure is defined as follows:
For example:
1 2 3 4 5 6 | struct fieldbite { unsigned short first : 2; unsigned short second: 2; unsigned short third : 4; } field; |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include <iostream> 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 для секунд }; int main() { 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; } |
Result:
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).
misinform people. The size
{
unsigned short first : 2;
unsigned short second: 2;
unsigned short third : 4;
} equal not 1 ʙajtu, and 2
2+2+4 = 8bit, 8bit = 1 ʙajtu Privet, I have a programmer Nurse?))
No, he said all right, 2 bytes.
If you put the unsigned char, then one byte, But now two. Believe in any ide.
And if int, then 4; and similarly for vosmibaytovyh.
struct test{
unsigned long long first : 64;
unsigned char second: 8;
}; – It has a size 16 bytes instead of the expected of you 9.
He aligns the size of the type of the maximum size, apparently.
Align
try
#pragma pack(push,1)
typedef struct{
unsigned short first :2;
unsigned short second:2;
unsigned short third :4;
} foo;
#pragma pop()
Although so 1 bytes in mingv msvts and hzz and relish
Here is some very important clarification
Using such a mechanism slows down the program
This is related to, what will be generated by the compiler for data manipulation(these will be bitwise operations), these will be bitwise operations(these will be bitwise operations(these will be bitwise operations, what is it, these will be bitwise operations))
these will be bitwise operations – this may not be a significant increase in time, but if the frequency of using such data goes off scale, then we will get, perhaps, cool program, which requires little RAM, but definitely a slow program
So use caution and weigh your decision.
Sergei, I study you, I pay money not to you. I will give you a recommendation, for you to teach. you are a good teacher. full respect for you