如何为以下内容设置/取消设置枚举值。使用 gcc,我收到了这个烦人的警告:
test.c:37: warning: negative integer implicitly converted to unsigned type
test.c:39: warning: negative integer implicitly converted to unsigned type
test.c:41: warning: negative integer implicitly converted to unsigned type
test.c:43: warning: negative integer implicitly converted to unsigned type
代码是:
#include <stdio.h>
#include <string.h>
typedef enum {
ONE = 0x1,
TWO = 0x2,
THREE = 0x4,
FOUR = 0x8,
} options;
static const char *byte_to_binary (int x)
{
int z;
static char b[9];
b[0] = '\0';
for (z = 256; z > 0; z >>= 1)
{
strcat(b, ((x & z) == z) ? "1" : "0");
}
return b;
}
int main(int argc, char *argv[])
{
options o = 0;
printf( "%s\n", byte_to_binary(o));
o |= ONE;
printf( "%s\n", byte_to_binary(o));
o |= TWO;
printf( "%s\n", byte_to_binary(o));
o |= THREE;
printf( "%s\n", byte_to_binary(o));
o |= FOUR;
printf( "%s\n", byte_to_binary(o));
o &= ~FOUR;
printf( "%s\n", byte_to_binary(o));
o &= ~THREE;
printf( "%s\n", byte_to_binary(o));
o &= ~TWO;
printf( "%s\n", byte_to_binary(o));
o &= ~ONE;
printf( "%s\n", byte_to_binary(o));
return 0;
}