Edit Anything

Professional text and hex editing
with Binary Templates technology.






010 Editor - Text/Hex Editor Homepage

A bitfield allows template variables to be subdivided into groups of bits. This process allows multiple variables to be packed into a single block of memory. The syntax for defining a bitfield is 'type_name <variable_name> : number_of_bits'. The type can be char, short, int, or int64 (unsigned or signed) or any of the equivalent types. If the variable name is omitted, the given number of bits will be skipped. For example,

    int alpha : 5;
    int       : 12;
    int beta  : 15; 

would pack alpha and beta into one 32-bit value, but skip 12 bits in the middle. 010 Editor has two special bitfield modes that determine how the bits are packed into variables: padded bitfields and unpadded bitfields.


Padded Bitfields

With padded bitfields (the default), how the bits are packed into a variable depends upon the current endianness. By default, bits are packed right-to-left for little endian files, and left-to-right for big endian files. For example, for the bitfields:

    ushort a : 4;
    ushort b : 7;
    ushort c : 5;

In little endian mode, this structure would be stored as the bits:

    cccccbbb bbbbaaaa

(and stored on disk as bbbbaaaa cccccbbb). In big endian mode, this structure would be stored as the bits:

    aaaabbbb bbbccccc

(and stored on disk as aaaabbbb bbbccccc). Whether the bits are packed left-to-right or right-to-left can be controlled by the functions BitfieldLeftToRight, and BitfieldRightToLeft.

In this mode, the program will automatically add padding bits when needed. If the size of the type being defined changes, padding will be added so the bitfield will be defined on the next variable boundary. Also, if the specified bitfield would step across the boundary of a variable, padding is added so the bitfield starts at the next variable. For example:

    int   apple   : 10;
    int   orange  : 20;
    int   banana  : 10; 
    int   peach   : 12;
    short grape   : 4;

The bitfields apple and orange would be packed into one 32 bit value. However, banana steps over the variable boundary, so 2 bits of padding are added so that it starts at the next 32 bit value. Banana and peach are packed into another 32-bit value, but because the size of the type changes with grape, an extra 10 bits of padding is added before grape is defined.


Unpadded Bitfields

010 Editor includes a special unpadded bitfield mode that treats the file as one long bit stream. No padding bits are added if the variable type changes or if the bits cannot be packed into a single variable. The unpadded mode can be entered by calling the function BitfieldDisablePadding (padding can be turned back on by calling BitfieldEnablePadding).

In unpadded bitfield mode, each variable defined reads some bits from the bitstream. For example:

    BitfieldDisablePadding();
    short a : 10;
    int   b : 20;
    short c : 10;

Here a reads the first 10 bits from the file, b reads the next 20 bits from the file, and so on. If the bitfields are defined as reading from right to left (this is the default for little endian data and can enabled using the function BitfieldRightToLeft), the variables would be stored as the bits:

     aaaaaaaa bbbbbbaa bbbbbbbb ccbbbbbb cccccccc

If the bitfields are defined as reading from left to right (this is the default for big endian data and can enabled using the function BitfieldLeftToRight), the variables would be stored as the bits:

     aaaaaaaa aabbbbbb bbbbbbbb bbbbbbcc cccccccc

When declaring structures containing unpadded bitfields, no extra padding bits are added between the structures. (Note that internally, any unpadded right-to-left bitfield is forced to be declared in little endian mode and any unpadded left-to-right bitfield is forced to be declared in big endian mode.)


Bitfields and Enums

Bitfields may be combined with enums by placing a colon and a number of bits after the enum definition. For example, to pack two enums into a single ushort, the following may be used:

     enum <ushort> ENUM1 { VAL1_1=25, VAL1_2=29, VAL1_3=7 } var1 : 12;
     enum <ushort> ENUM2 { VAL2_1=5,  VAL2_2=6 }            var2 : 4;

This is the manual for 010 Editor, a professional hex editor and text editor. Use 010 Editor to edit the individual bytes of any binary file, hard drive, or process on your machine. 010 Editor contains a whole host of powerful analysis and editing tools, plus Binary Templates technology that allows any binary format to be understood.





Newsletter - Receive special offers, tips, tricks and news. Join now



010 Editor v14.0.1 is here!
What's new?


Navigation


Products

010 Editor














E-mail: info@sweetscape.com