might take longer. This compromise may be considered a form of space–time tradeoff. Although use of "packed" structures is most frequently used to conserve memory space, it may also be used to format a data structure for transmission using a standard protocol. However, in this usage, care must also be taken to ensure that the values of the struct members are stored with the endianness required by the protocol (often network byte order), which may be different from the endianness used natively by the host machine. Computing padding The following formulas provide the number of padding bytes required to align the start of a data structure (where mod is the modulo operator): padding = (align - (offset mod align)) mod align aligned = offset + padding = offset + ((align - (offset mod align)) mod align) For example, the padding to add to offset 0x59d for a 4-byte aligned structure is 3. The structure will then start at 0x5a0, which is a multiple of 4. However, when the alignment of offset is already equal to that of align, the second modulo in (align - (offset mod align)) mod align will return zero, therefore the original value is left unchanged. Since the alignment is by definition a power of two, the modulo operation can be reduced to a bitwise boolean AND operation. The following formulas produce the correct values (where & is a bitwise AND and ~ a bitwise NOT) -- providing the offset is unsigned or the system uses two's complement arithmetic: padding = (align - (offset & (align - 1))) & (align - 1) = -offset & (align - 1) aligned = (offset + (align - 1)) & ~(align - 1) = (offset + (align - 1)) & -align Typical alignment of C structs on x86 Data structure members are stored sequentially in memory so that, in the structure below, the member Data1 will always precede Data2; and Data2 will always precede Data3: struct MyData { short Data1; short Data2; short Data3; }; If the type "short" is stored in two bytes of memory then each member of the data structure depicted above would be 2-byte aligned. Data1 would be at offset 0, Data2 at offset 2, and Data3 at offset 4. The size of this structure would be 6 bytes. The type of each member of the structure usually has a default alignment, meaning that it will, unless