S
Serve Laurijssen
What would be an optimal way to get the position of the lowest bit where you
know only 1 bit will be on 1?
Like in the position of bit 0x8 is 4, 0x4 it's 3 etc.
Best thing I could come up with is
#define BITPOS(x) \
((x) & 0x1 ? 1 : \
(x) & 0x2 ? 2 : \
(x) & 0x4 ? 3 : \
(x) & 0x8 ? 4 : 0)
int main(void) {
unsigned short x = 0x0008;
while (x) {
printf("%d\n", BITPOS(x));
x >>= 4;
}
return 0;
}
know only 1 bit will be on 1?
Like in the position of bit 0x8 is 4, 0x4 it's 3 etc.
Best thing I could come up with is
#define BITPOS(x) \
((x) & 0x1 ? 1 : \
(x) & 0x2 ? 2 : \
(x) & 0x4 ? 3 : \
(x) & 0x8 ? 4 : 0)
int main(void) {
unsigned short x = 0x0008;
while (x) {
printf("%d\n", BITPOS(x));
x >>= 4;
}
return 0;
}