RE: Bit masking and shifts 09-04-2015, 01:14 AM
#20
The other trick I use bitshifts for is to efficiently round down to a certain value. Most compilers will use similar tricks in the background if you don't keep your stack properly aligned to optimize for performance between your local variable allocation's through manipulations to the stack pointer directly and any push instructions... But here's an example:
This will take away from x the proper value to round the value down to the nearest multiple of 16, in this case 32.
Code:
#include <stdio.h>
int main(void)
{
int x = 42;
x &= 0xFFFFFFF0;
printf("%d\n", x);
return 0;
}This will take away from x the proper value to round the value down to the nearest multiple of 16, in this case 32.



![[+]](https://sinister.li/images/modern/collapse_collapsed.png)