Bit field
In information technology and programming , a bit field denotes an unsigned integer in which individual bits or groups of bits are strung together. It represents a kind of compound data type on bit level. In contrast to this, there is the primitive data type , in which the value is formed from all digits together.
A common usage is where every single binary digit represents a flag . Each position corresponds to a Boolean variable . However, several digits can also form a value, e.g. For example, two nibbles can be combined in one byte or, as with IPv4 addresses, a 32-bit data word can be divided into (for example) 24-bit network and 8-bit host parts.
There is also the phrase “bit vector”, without always expressing that the individual bit can be addressed by indexing . Bit indexing is covered in the bit string article .
use
Bit fields are typically used in hardware-related or system programming . Here they are often used to set a certain behavior for peripheral devices. This is because, for example, the activation of a certain functionality in hardware can easily be signaled by setting a single data line, a bit. In order to communicate more easily with the software or other devices or because, for example, there are only a limited number of I / O ports, several such lines are then combined into one data word, the bit field.
In addition, bit fields are also used in application programming , where a large number of parameters are to be transferred. The readability of the source code can be improved here if, instead of a long parameter list in which each parameter must be specified explicitly, only a bit field with the required flags is put together.
Using bit fields to save memory space doesn't make that much sense in application programming these days, except in rare cases, such as B. in embedded systems , when memory is an extremely scarce resource, or when the bits are needed in extraordinarily large numbers, as in the sieve of Eratosthenes . A whole byte or word is usually used for Boolean variables. Access to individual bits of a data word is also often more inefficient than to the entire data word.
Example in OpenGL
In OpenGL , for example, the function glClear is defined, which clears one or more of four graphic buffers. The developers could now have defined four parameters , each of which specifies whether the graphics buffer should be cleared or not. The function call would look like this:
void glClear(1, 1, 0, 0);
However, this is neither efficient, since four variables have to be passed, nor is it very legible. For this reason, a so-called flag was defined for each buffer in the gl.h :
#define GL_DEPTH_BUFFER_BIT 0x00000100
#define GL_ACCUM_BUFFER_BIT 0x00000200
#define GL_STENCIL_BUFFER_BIT 0x00000400
#define GL_COLOR_BUFFER_BIT 0x00004000
And only a single parameter was defined for the function:
void glClear(GLbitfield mask); // GLbitfield ist ein typedef auf unsigned int
The function call now looks like this:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Use in C and C #
In the C programming language , it is possible to define bit fields in data structures and thus to save smaller data types in a compact manner. As long as the data is addressed exclusively via the field names, the source code does not depend on the compiler or processor . This does not apply to access to parts of registers or deserialized data.
struct struct-type-name {
type [name1] : length;
type [name2] : length;
//...
type [nameN] : length;
} variable-list;
In the C # programming language it is possible to use the flags attribute to declare an enumeration as a bit field. In addition, the .NET Framework , the data structure BitArray available which stores individual bits compact and allows Boolean operations.
access
Access to a single bit, both in testing and setting, is supported by the hardware just as well as access to a byte or word - a single command is sufficient for many machines. The support provided by the compilers is often similar to access to several bits, in which the bit group has to be "prepared" from the memory word using a bit mask before it can be compared or manipulated. As Bitmasks bit fields are designated whose points represent himself no information but which are used to read or manipulate bit fields.
An example of a bit mask that is widespread in network technology is the network mask . It determines which of the leading bits of an IP address are the network prefix, i. H. which address range is regarded as internal and which as external network when routing.
Read out bit
To read out one or more bits from a bit field, it is linked with a bit mask using the bit-by-bit AND operation . In the bit mask there is a "0" in all places that are not to be considered. These positions are hidden by the AND link, i. H. set to zero. The places that are to be viewed or read out contain a "1" in the bit mask and are therefore taken over unchanged from the bit field in the result.
example
1-bit:
01001011 Bitfeld AND 00001000 Bitmaske ------------- = 00001000 Ergebnis
0-bit:
01001011 Bitfeld AND 00000100 Bitmaske ------------- = 00000000 Ergebnis
The result is zero if the positions of the bit mask in the bit field are also zero. If at least one of them is set to "1", the corresponding bits are also "1" in the result, i. H. the result is not zero.
Since values from zero are evaluated in most programming languages to a logical false and values not equal to zero to a logical true , the result can now easily be checked with a conditional branch to see whether one of the bits of the bit mask was set in the field:
if (Ergebnis)
// Bit gesetzt
else
// Bit nicht gesetzt
Set bits
If bits in the bit field are to assume a certain value, the corresponding positions in the bit field must be set to "1" and all others to "0". To set the digits to "1", the bit field must be added to the bit mask or linked via a bit-wise OR . Conversely, you get a "0" at the desired places if you link the field and mask via a NAND , i.e. first invert the mask and then connect it to the field using an AND operation.
example
Setting bits to "1":
01001011 Bitfeld OR 00000100 Bitmaske ------------- = 01001111 Ergebnis
Setting bits to "0":
NOT 00001000 Bitmaske ------------- = 11110111 invertierte Bitmaske AND 01001011 Bitfeld ------------- = 01000011 Ergebnis
Toggle bits
If individual bits switched ( English to toggle ) or be inverted is to the appropriate locations in the bit mask to "1" and all others to "0" and bit mask associated bit field and an exclusive or .
01001011 Information XOR 00000110 Bitmaske ------------- = 01001101 Ergebnis
Summarize bit masks
Bit masks can be combined, e.g. B. to check or set several bits at once in one operation. To do this, you simply add the individual bit masks or link them using a bit-wise OR.
00000001 Bitmaske1 OR 00000010 Bitmaske2 ------------- 00000011 zusammengefasste Bitmaske
See also
Individual evidence
- ↑ The use of bit fields of any size and with an index variable is discussed in more detail in the article Bit string .
- ↑ http://msdn.microsoft.com/de-de/library/system.flagsattribute.aspx