3 ms·
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> vo
by School-Cotton 2y ago
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);
}