比特运营实施

时间:2014-02-06 05:41:28

标签: c bits operation

如果设置了x中的位i,则必须返回1,否则返回0,否则返回is_set函数。我被困在这里。不知道接下来要做什么......有什么想法?任何帮助将不胜感激....

#include <string.h>
#include <stdio.h>

const char *to_binary(unsigned int x) {
    static char bits[17];
    bits[0] = '\0';
    unsigned int z;
    for (z = 1 << 15; z > 0; z >>= 1) {
        strcat(bits, (x & z) ? "1" : "0");
    }
    return bits;

1 个答案:

答案 0 :(得分:2)

short is_set(unsigned short x, int bit) {
    return x & (1 << bit) ? 1 : 0; 
}

可替换地,

short is_set(unsigned short x, int bit) {
    return (x >> bit) & 1; 
}