3 ms·
I work on protocol engineering and one of the protocols I support requires fields to be padded at 4-byte word boundaries. The code I inherited uses a grungy mac
by nathanb 12y ago
I work on protocol engineering and one of the protocols I support requires fields to be padded at 4-byte word boundaries. The code I inherited uses a grungy macro to solve this problem:
#define WORD_ALIGN_LEN(l) (l % 4 ? (((l/4)*4) + 4) : l)
So for all you complaining about his solution being difficult to mentally parse: it could be worse.
(Our code will divide by 4 and then multiply by 4, truncating the remainder, and then add 4. So if we're sending 6 bytes, we'll divide by 4 to get 1, multiply to get 4, then add 4 to get 8.)