How would one set/unset enum value for something like the following. Using gcc, I get this annoying warning:
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
code is:
#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;
}
Since your enum does not contain any negative integer constants, I guess GCC has given unsigned
int type to your enums. Now the expressions like
o &= ~FOUR
are equivalent to
o = o & ~FOUR
On the RHS, o
is unsigned int and ~FOUR
is signed int and by type conversion rules, signed int will be converted to unsigned int. Also ~FOUR
is a negative number and hence you get a warning for implicit conversion of a negative number to unsigned type.
If you are sure of your logic, you need not worry about the warnings or you can convert your enum to signed by having a dummy enum
which equals to a negative number.
Something like
typedef enum {
DUMMY =-1,
ONE = 0x1,
TWO = 0x2,
THREE = 0x4,
FOUR = 0x8,
} options;
Also, your code has run-time buffer overflow problems. In function byte_to_binary
you are checking for 9 bits, but your buffer is also 9 bytes. It has to be 10 bytes, one for the terminating null. Make it static char b[10];
and everything works fine