Mask (computing)

In computer science, a bit mask denotes a multi-digit binary number can be read or stored using the information from another binary number.

  • 2.1 memory savings
  • 2.2 Named flags 2.2.1 bitfields

Methods

Read bit

Example of 1-bit:

01001011 information AND bit mask 00001000 ------------- = 00001000 result Example of 0 -bit:

01001011 information AND bit mask 00000100 ------------- = 00000000 result Having a simple condition can now be checked for the bit:

IF result == 0   THEN " bit 0 "   ELSE " bit 1" Set bit 0

NOT bitmask 00001000 ------------- = 11110111 inverted bitmask      01001011 information AND 11110111 inverted bitmask ------------- = 01000011 result Set 1-bit

01001011 information OR bit mask 00000100 ------------- = 01001111 result Bit switch ( toggle)

01001011 information XOR bitmask 00000110 ------------- = 01001101 result An XOR example:

A XOR B 10 XOR 255 = 245 245 XOR 255 = 10 Summarize bitmasks

00000001 Bitmaske1 OR 00000010 Bitmaske2 -------------      00000011 combined bitmask create bitmask

Most programming languages ​​support logical shift, thus very efficient masks can be created with just one bit. In the following example a bit mask with a bit set at the fourth position (00010000) is created.

Unsigned int bitmask = 1 << 4; / / Bitmask set to 00010000 However, if the bit map is already known before the program execution, and the compiler does not constant folding, it would be more efficient to define the bit mask as a constant.

A list of 16 bits in hexadecimal notation

# define Bitmask01 0x00000001   # define Bitmask02 0x00000002   # define Bitmask03 0x00000004   # define Bitmask04 0x00000008   # define Bitmask05 0x00000010   # define Bitmask06 0x00000020   # define Bitmask07 0x00000040   # define Bitmask08 0x00000080   # define Bitmask09 0x00000100   # define Bitmask10 0x00000200   # define Bitmask11 0x00000400   # define Bitmask12 0x00000800   # define Bitmask13 0x00001000   # define Bitmask14 0x00002000   # define Bitmask15 0x00004000   # define Bitmask16 0x00008000 Practical use

Memory savings

Since, in the programming due to the internal structure of the computer for a variable is always at least one byte has to be used ( for truth values), it would be inefficient for any information required by the only one bit to use a complete byte. A bit mask up to eight bits can be accessed in one byte. However, only logically related bits are in practice usually stored in one byte (see example: named flags). The memory saving is mainly engaged in hardware programming or network protocols necessary because there space is very scarce. In the application programming is the cost of saving usually be out of all proportion to the benefit, as modern personal computer through several gigabytes of memory available (as of 2013).

Named flags

In OpenGL, the glClear function is defined, for example, which deletes one or more of four graphics buffers. The developers would now be able to define four parameters, each indicating whether the graphics buffer should be cleared or not. The function call would look like this:

Void glClear (1, 1, 0, 0); / / Attention! This function call does not exist However, this is not efficient, since four variables need to be passed, still very legible. Therefore, a so called flag was in the gl.h for each buffer defined:

# 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 for the function of only a single parameter was defined:

Void glClear ( GLbitfield mask ); / / Typedef int GLbitfield is to unsigned The function call looks like this:

GlClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); Internally, the function might look as follows:

Void glClear ( GLbitfield mask ) {      if ( mask & GL_COLOR_BUFFER_BIT ) {         / / Clear color buffer      }      if ( mask & GL_DEPTH_BUFFER_BIT ) {         / / Clear depth buffer      }      if ( mask & GL_ACCUM_BUFFER_BIT ) {         / / Clear accumulation buffer      }      if ( mask & GL_STENCIL_BUFFER_BIT ) {         / / Clear stencil buffer      }   } bitfields

In the C programming language, it is also possible to use bit fields that allow the direct access to individual bits. It is important to note that the code easier processor- dependent, since they are influenced by Little- and big-endian architectures.

Struct struct - type-name {       type [ name1 ]: length;       type [ name2 ]: length;       / / ...       type [ nameN ]: length;   Variable -list }; In the C # programming language, it is possible by means of the flags attribute to declare an enum as a bit field. Moreover, the. NET Framework data structure BitArray available which compactly stores individual bits and Boolean operations allow.

Further application examples

  • Netmask
  • Key input mask
129719
de