https://stackoverflow.com/questions/11642210/computing-padding-required-for-n-byte-alignment 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.