What\'s a fast way to round up an unsigned int
to a multiple of 4
?
A multiple of 4 has the two least significant bits 0, right? So I could
If you want the next multiple of 4 strictly greater than myint
, this solution will do (similar to previous posts):
(myint + 4) & ~3u
If you instead want to round up to the nearest multiple of 4 (leaving myint
unchanged if it is a multiple of 4), this should work:
(0 == myint & 0x3) ? myint : ((myint + 4) & ~3u);