简体   繁体   中英

Need to map enum of bits like bit 0 indicates error 1, bit 1 indicates error2, with int binary sequence in C

Need to map enum of bits like (error is basically some string) bit 0 indicates error 1, bit 1 indicates error2, bit 2 indicates error3, . . . . bit n indicates error n

decimal value like int value= 283192 or any other value.

I tried by converting 283192 = 1000101001000111000.

Need help how to map this sequence with above enum using C.

Thanks in advance

Not sure this is what you are talking about, but that's a way to indicate which bits are set:

unsigned int bitmap = value;
unsigned int i = 1;
while (bitmap) {
    if (bitmap & 1) {
        printf("Error number %i\n", i);
    }
    bitmap >>= 1;
    ++i;
}

Like this?

enum bad_errorcode {
  BADERR_NOBITS = 0,
  BADERR_BITONE = 1 << 0,
  BADERR_BITTWO = 1 << 1,
  BADERR_BITTHREE = 1 << 2,
  BADERR_BITFOUR = 1 << 3,
  /* ... */
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM