Bit Structures

Bit structures provide a tool for accessing individual bits.

bitstruct FlagDword {

This declaration introduces two one bit fields Flag1 and Flag2, and one three bit field Mask. Field values are treated as unsigned decimals, so Flag1 and Flag2 may be either 0 or 1, and Mask accepts value from 0 to 7.

The keyword bitstruct may be preceded by a size specifier byte, word, dword, or qword. If the size keyword is omitted, FlexHEX assumes the size is 32-bit; for instance, the above declaration defines a 32-bit (double word) structure.

Bits are allocated incrementally starting from the least significant bit. In the above example Flag1 occupies bit 0, Flag2 - bit 1, and Mask - bits 2 to 4. In order to skip some bits use the unused keyword. For example, the following declaration:

byte bitstruct FlagByte {

will create a byte-sized bit structure with the bits positioned as follows:

  • Bit 0 is not used.
  • Bit flag Valid occupies bit 1.
  • Bit flag Locked occupies bit 2.
  • Bits 3 and 4 are not used.
  • The three-bit long number nLocks occupies bits 5 to 7.

Anonymous Bit Structures

Like other composite data structures, bit structures may be anonymous, that is used without declaring an associated type. An anonymous bit structure lacks the type name between the bitstruct keyword and the opening curly bracket, and can be used wherever a type name is expected.

struct DocumentRecord {
  FILETIME CreationTime;
  byte bitstruct {
  } Status;
  char szName[];