c++ - Explanation needed for round_down macro -
in
#define round_down(x, s) ((x) & ~((s)-1))
i don't quite understand part ((x) & ~((s)-1))
. thankful explanation.
so, usage of is:
round_down(152, 128);
let's @ happens:
~(128 - 1) == ~(127) == ~(0x7f) == 0xffffff80
so, appears masking out lower bits of number.
round_down(152, 128) == (152 & 0xffffff80) == 128 round_down(300, 128) == (300 & 0xffffff80) == 256
also, s
must power of 2. macro doesn't make mathematical sense otherwise.
Comments
Post a Comment