RE: Bit masking and shifts 09-04-2015, 01:22 AM
#21
(09-04-2015, 01:14 AM)0xDEAD10CC Wrote: 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:
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.
This is kind of smart and neat, I like it, thanks for sharing mate.


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