Data structure alignment: Difference between revisions

Content deleted Content added
style
Line 86:
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 [[#Definitions|definition]] a power of two,{{efn|On modern computers where the target alignment is a power of two. This might not be true, for example, on a system using 9-bit bytes or 60-bit words.}} the modulo operation can be reduced to a bitwise booleanBoolean 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: