Hacker News new | ask | show | jobs
by bitwize 4296 days ago
Holy shit, mind blown.

I've done this before, but usually take modulo 8 rather than bitwise-and negative 7 as the final step.

2 comments

Doesn't matter, any major compiler will transform this into an bitwise and.
Same, I'd write it out using modulo and would assume the compiler would figure it out.

    void *p = x;
    p += 7;
    p -= (p%8);
Now that I've written it out I suppose it's not any clearer than the mask method:

    void *p = x;
    p += 7;
    p &= ~7;