What is the best way to calculate number of padding bytes

Viewed 7714

What's the most efficient way to calculate the amount of padding for 8-bit data that needs to be a multiple of 32-bit in C?

At the moment I do it like this:

pad = (4-size%4)%4;
3 Answers

pad = (-size)&3;

This should be the fastest.

size 0: pad 0
size 1: pad 3
size 2: pad 2
size 3: pad 1
Related