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

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -