All powers of two (1, 2, 4, 8, 16, 32 ...) can be aligned using simple operations a and.
In this case, the size is rounded:
size &= ~(alignment - 1);
or if you want to round:
size = (size + alignment-1) & ~(alignment-1);
"alignment-1", if this value is two, it will give you "all" up to a bit just below two. ~ inverts all bits, so you get them for zeros and zeros for them.
You can verify that something has the power of two:
bool power_of_two = !(alignment & (alignment-1))
This works because, for example, 4:
4 = 00000100 4-1 = 00000011 & -------- 0 = 00000000
or for 16:
16 = 00010000 16-1 = 00001111 & -------- 0 = 00000000
If instead use 5:
5 = 00000101 4-1 = 00000100 & -------- 4 = 00000100
So not the power of two!
source share