Bit fields in C ++

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:

Битовые поля в С++

C++

1

2

3

4

5

6

structfieldbite

{

unsignedshortfirst:2;

unsignedshortsecond:2;

unsignedshortthird: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 unsignedshortfifth: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.

Битовые поля в C++

C++

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>

usingnamespacestd;

structDateTime//Опишем структуру с битовыми полями

{

unsignedshortDay:5;//5 бит для дня

unsignedshortMonth:4;//4 для месяца

unsignedshortYear:7;//7 для года от 0 до 99

unsignedshortHour:5;//5 бит для 24-х часов

unsignedshortMinute:6;//6 для минут

unsignedshortSecond:6;//6 для секунд

};

intmain()

{

DateTimed;//объявляем переменную этого типа с битовыми полями

inti;//И еще одну, в которую будет поступать ввод данных

//Введем дату

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).