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

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -