Joy
2014-10-03 17:09:06 UTC
I am trying to write a program which reverse bits with lookup table(16elements)
?? Here is my code ,but it only works for 1~800
It cannot work for a large number like 80000000
Why?
Question2
How can I replace Uchar_t * pa, * pb; with a union?
Thank you very much
#include
#include
#include
#include
typedef enum{FALSE,TRUE} Bool_t;
typedef unsigned char Uchar_t;
typedef unsigned int Uint_t;
/* main program reads in a value in hex to reverse
if the value is zero, the program stops */
Uchar_t lookup[16]={
0x0, 0x8, 0x4, 0xC,
0x2, 0xA, 0x6, 0xE,
0x1, 0x9, 0x5, 0xD,
0x3, 0xB, 0x7, 0xF };
int main(int argc, char ** argv){
Uint_t val, z;
Uchar_t * pa, * pb; /* can you replace this pointer stuff with a union? */
pa = (Uchar_t *) &val;
pb = (Uchar_t *) &z;
int i;
while(TRUE){
printf("Value to reverse in hex: ");
scanf("%x", &val);
if(!val) return FALSE; /* zero value exits */
for(i=0;i<=3;i++) *(pb+i) = lookup[*(pa+3-i)]<<4;
printf("Reverse is: %08x\n",z);
}
}