C solution (0ms) - Lookup using a bitmap table


  • 0
    #define UINT8_LEN 256
    uint8_t bitMap[UINT8_LEN];
    
    uint8_t reverseUint8(uint8_t byte) {
        byte = (byte & 0xAA) >> 1 | (byte & 0x55) << 1;
        byte = (byte & 0xCC) >> 2 | (byte & 0x33) << 2;
        return (byte & 0xF0) >> 4 | (byte & 0x0F) << 4;
    }
    
    uint32_t reverseBits(uint32_t word) {
        return reverseUint8(word >> 24) |
               reverseUint8(word >> 16) << 8 |
               reverseUint8(word >> 8) << 16 |
               reverseUint8(word) << 24;
    }
    
    // The bitmapInit() function gets invoked dynamically when main (or the shared lib) gets loaded.
    void bitmapInit(void) __attribute__ ((constructor));
    void bitmapInit(void) {
        for (int i = 0; i < UINT8_LEN; i++) bitMap[i] = reverseUint8(i);
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.