Hacker News new | past | comments | ask | show | jobs | submit login

One reason why this is useful is it lets you iterate through the set bits of a number.

For example:

    #include <stdlib.h>
    #include <stdio.h>
    
    void decompose_bits(unsigned x) {
        while (x) {
            unsigned bit = x & -x;
            printf("0x%x (%u)\n", bit, bit);
            x -= bit;
        }
    }
    
    int main(int argc, char *argv[]) {
        unsigned x = atoi(argv[1]);
        decompose_bits(x);
    }



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: