Determine which single bit in the byte is set

后端 未结 8 987
臣服心动
臣服心动 2020-12-03 18:53

I have a byte I\'m using for bitflags. I know that one and only one bit in the byte is set at any give time.

Ex:

8条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-03 19:21

    A lookup table is fast and easy when CHAR_BIT == 8, but on some systems, CHAR_BIT == 16 or 32 and a lookup table becomes insanely bulky. If you're considering a lookup table, I'd suggest wrapping it; make it a "lookup table function", instead, so that you can swap the logic when you need to optimise.

    Using divide and conquer, by performing a binary search on a sorted array, involves comparisons based on log2 CHAR_BIT. That code is more complex, involving an initialisation of an array of unsigned char to use as a lookup table for a start. Once you have such the array initialised, you can use bsearch to search it, for example:

    #include 
    #include 
    void uchar_bit_init(unsigned char *table) {
        for (size_t x = 0; x < CHAR_BIT; x++) {
            table[x] = 1U << x;
        }
    }
    int uchar_compare(void const *x, void const *y) {
        char const *X = x, *Y = y;
        return (*X > *Y) - (*X < *Y);
    }
    size_t uchar_bit_lookup(unsigned char *table, unsigned char value) {
        unsigned char *position = bsearch(lookup, c, sizeof lookup, 1, char_compare);
        return position ? position - table + 1 : 0;
    }
    int main(void) {
        unsigned char lookup[CHAR_BIT];
        uchar_bit_init(lookup);
        for (;;) {
            int c = getchar();
            if (c == EOF) { break; }
            printf("Bit for %c found at %zu\n", c, uchar_bit_lookup(lookup, c));
        }
    }
    

    P.S. This sounds like micro-optimisation. Get your solution done (abstracting the operations required into these functions), then worry about optimisations based on your profiling. Make sure your profiling targets the system that your solution will run on if you're going to focus on micro-optimisations, because the efficiency of micro-optimisations differ widely as hardware differs even slightly... It's usually a better idea to buy a faster PC ;)

提交回复
热议问题