Fast value reverting function for 8-bit char and 16-bit integer, it uses a lookup table for each nibble (4-bits).
Alex
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 /***************************************************************************** ** Function name: reverse_byte ** ** Descriptions: reverts the bits of a byte b0->b7, b1->b6, b2->b5 etc ** ** parameters: unsigned char input_byte : the values that will be inverted ** Returned value: unsigned char : the reverted result ** *****************************************************************************/ unsigned char reverse_byte (unsigned char input_byte) { const unsigned char lookup[16]={0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15}; // depending on the compiler use the proper keyword to make this a flash (ROM) variable return (lookup[input_byte & 15]<<4) | (lookup[input_byte>>4]); } /***************************************************************************** ** Function name: reverse_int ** ** Descriptions: reverts the bits of a byte b0->b15, b1->b14, b2->b13 etc ** ** parameters: unsigned int input_int : the values that will be inverted ** Returned value: unsigned int : the reverted result ** *****************************************************************************/ unsigned int reverse_int (unsigned int input_int) { const unsigned char lookup[16]={0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15}; // depending on the compiler use the proper keyword to make this a flash (ROM) variable return (lookup[input_int & 15]<<12) | (lookup[(input_int>>4) & 15]<<8) | (lookup[(input_int>>8) & 15]<<4) | (lookup[(input_int>>12)]); }
Alex