How to reverse a Unsigned Char??

R

ranjanmg1

I have a unsigned char.. i need to reverse it.. what the easiest way to
do it?? i dont want to tap each bit save and restore etc etc....

Is it possible to perform some bitwise operation which will give the
reversed char.

e.g., unsigned char x = 0x8A
am expecting an output after reversal x = 0x51
 
M

mark_bluemel

I have a unsigned char.. i need to reverse it.. what the easiest way to
do it?? i dont want to tap each bit save and restore etc etc....

Is it possible to perform some bitwise operation which will give the
reversed char.

Go to Google Groups and search for "reverse" in comp.lang.c
 
J

Jens Thoms Toerring

I have a unsigned char.. i need to reverse it.. what the easiest way to
do it?? i dont want to tap each bit save and restore etc etc....
Is it possible to perform some bitwise operation which will give the
reversed char.
e.g., unsigned char x = 0x8A
am expecting an output after reversal x = 0x51

There's no operator or standard function for this in C, but if you
google for 'reverse bits in a byte' you will find e.g.

http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious

and lots of other results.
Regards, Jens
 
J

Jordan Abel

2006-10-24 <[email protected]>,
I have a unsigned char.. i need to reverse it.. what the easiest way to
do it?? i dont want to tap each bit save and restore etc etc....

Is it possible to perform some bitwise operation which will give the
reversed char.

e.g., unsigned char x = 0x8A
am expecting an output after reversal x = 0x51

static unsigned char CSStab4[256]=

{
0x00,0x80,0x40,0xc0,0x20,0xa0,0x60,0xe0,
0x10,0x90,0x50,0xd0,0x30,0xb0,0x70,0xf0,
....
And so on in much the same manner.

x = 0x8a;
x[CSStab4] == 0x51;
 
P

pete

I have a unsigned char.. i need to reverse it.. what the easiest way to
do it?? i dont want to tap each bit save and restore etc etc....

Is it possible to perform some bitwise operation which will give the
reversed char.

e.g., unsigned char x = 0x8A
am expecting an output after reversal x = 0x51

unsigned char bit_rev(unsigned char byte)
{
unsigned hi_mask = ((unsigned char)-1 >> 1) + 1;
unsigned lo_mask = 1;

do {
if (!(byte & hi_mask) != !(byte & lo_mask)) {
byte ^= hi_mask | lo_mask;
}
hi_mask >>= 1;
lo_mask <<= 1;
} while (hi_mask > lo_mask);
return byte;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top