c - Bits operations implementations -
i have return 1 if bit in x set, 0 otherwise in is_set function. got stuck here. have no idea next...any ideas?? appreciated....
#include <string.h> #include <stdio.h> const char *to_binary(unsigned int x) { static char bits[17]; bits[0] = '\0'; unsigned int z; (z = 1 << 15; z > 0; z >>= 1) { strcat(bits, (x & z) ? "1" : "0"); } return bits;
short is_set(unsigned short x, int bit) { return x & (1 << bit) ? 1 : 0; }
alternatively,
short is_set(unsigned short x, int bit) { return (x >> bit) & 1; }
Comments
Post a Comment