Z
zets
I need a macro for counting the bits in the odd positions of a given
input (of any type, char, pointer, int, struct, whatever).
Is there any clever way I could not think of, to do it efficiently?
#define COUNT(num, count) \
do { \
unsigned char *safe ## num ## count = (unsigned
char *)(&(num)); \
size_t safe ## size = sizeof(num); \
count=0; \
while (safe ## size--) { \
count+=(((*safe ## num ## count &
0x2)!=0)+((*safe ## num ## count & 0x8)!=0)+((*safe ## num ## count &
0x20)!=0)+((*safe ## num ## count & 0x80)!=0)); \
safe ## num ## count++; \
} \
} while (0)
Thanks
input (of any type, char, pointer, int, struct, whatever).
Is there any clever way I could not think of, to do it efficiently?
#define COUNT(num, count) \
do { \
unsigned char *safe ## num ## count = (unsigned
char *)(&(num)); \
size_t safe ## size = sizeof(num); \
count=0; \
while (safe ## size--) { \
count+=(((*safe ## num ## count &
0x2)!=0)+((*safe ## num ## count & 0x8)!=0)+((*safe ## num ## count &
0x20)!=0)+((*safe ## num ## count & 0x80)!=0)); \
safe ## num ## count++; \
} \
} while (0)
Thanks