Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Computing padding required for n-byte alignment

Tags:

math

Say I have a collection of data (eg: strings) that must be stored in a binary file padded so that each string is, say, 4-byte aligned.

So if I have a string of length 11, it would get padded to 12 (with null bytes).
If I have a string of length 24, then no padding is necessary.
If my string has a length of 6, it would get padded to 8 bytes.

How do I compute the amount of padding required in a single expression?

I tried 4 - (string_length % 4) but it fails when my string length is a multiple of 4.

like image 888
MxLDevs Avatar asked Jul 25 '12 03:07

MxLDevs


People also ask

What does 4 byte aligned mean?

For instance, in a 32-bit architecture, the data may be aligned if the data is stored in four consecutive bytes and the first byte lies on a 4-byte boundary. Data alignment is the aligning of elements according to their natural alignment.

Why does the compiler sometimes insert padding between fields and or at the end of a struct?

structure A If the short int element is immediately allocated after the char element, it will start at an odd address boundary. The compiler will insert a padding byte after the char to ensure short int will have an address multiple of 2 (i.e. 2 byte aligned).

What is structure alignment in C?

Data structure alignment is the way data is arranged and accessed in computer memory. Data alignment and Data structure padding are two different issues but are related to each other and together known as Data Structure alignment.

Do unions have padding?

In short, Yes!! On my Windows system, a union is padded to have a size that is a multiple of the size of an int ,ie 4 bytes.So unless you use #pragma pack(1) , a union having size 3 bytes will be padded with one more byte to yield 4 bytes.


2 Answers

This looks odd but gives the right answer:

(4 - (string_length % 4)) % 4
like image 153
Ned Batchelder Avatar answered Oct 04 '22 12:10

Ned Batchelder


There is a faster way to compute the padding, if the alignment is a power of two (2,4,8,...). The following runs because binary & is similar to % for powers of two: %(2^x) and &(2^x-1) do the same for positive numbers. Attention: & will delete the sign bit and therefore always returns the positive modulo result.

So (4 - (string_length & 3)) & 3 will do the same as (4 - (string_length % 4)) % 4. Using the positive modulo property this can be simplified to (-string_length) & 3!


If you wanna add that result to the size you can even do more optimizations:

padded_length = (string_length + 3) & ~3 Semantically this 'rounds up' the number to the padding size of 4.

like image 32
Johannes Jendersie Avatar answered Oct 04 '22 13:10

Johannes Jendersie