/* BEGIN rev_byte.c */
#include <stdio.h>
int main(void)
{
unsigned char byte, revbyte, hi_mask, lo_mask;
byte = 0;
do {
--byte;
revbyte = byte;
hi_mask = ((unsigned char)-1 >> 1) + 1;
lo_mask = 1;
do {
if (!(revbyte & hi_mask) != !(revbyte & lo_mask)) {
revbyte ^= hi_mask | lo_mask;
}
hi_mask >>= 1;
lo_mask <<= 1;
} while (hi_mask > lo_mask);
printf(" byte = %3u, revbyte = %3u\n",
(unsigned)byte, (unsigned)revbyte);
} while (byte);
return 0;
}
/* END rev_byte.c */
This seems to be a good solution, although as far as I understand it, it
doesn't really expand the solution space of the previous example that
much. Practically speaking, the possible number of bits for unsigned char
is 8, 16, 24, 32, 64. All of those are powers of two except 24, so the
power of two solution will work. Your solution, if I understand correctly,
will also work on the 24-bit unsigned char.
When I had to solve this problem, I wanted to support any number of bits.
Since I was doing this to support a naive radix 2 FFT, I didn't worry
about size. I figured that if the machine could handle the data it was
using for the FFT, it could handle the LUT, since they would be the same
size.
You inspired me to go back and look at what I did. It looks like I
specified the number of bits, and then copied one bit at a time into the
appropriate place in the LUT.
Here it is. I am the sole author of the following code for copyright
purposes, and I hereby formally release it into the public domain. (I
didn't check to see if it violates any software patents. ;-))
int *build_bitrev_lut(unsigned int num_pts, int num_bits)
/**********************************************
allocates array of size num_pts, and creates a
"bitrev" (bit reversing) look-up table (lut)
in the array. Once this is done, bit reversal
can be effected simply by using array indexing.
e.g., temp = data
; data=data[bitrev];
data[bitrev]=temp;
**********************************************/
{
int i, j, *bitrev;
bitrev = calloc(num_pts, sizeof *bitrev);
if (NULL == bitrev)
return NULL;
for (i=0; i<num_pts; i++)
for (j=0; j<num_bits; j++)
bitrev = (bitrev << 1) + ((i >> j) & 1);
return bitrev;
}
Mac
--