By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,795 Members | 1,216 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,795 IT Pros & Developers. It's quick & easy.

How to reverse a Unsigned Char??

P: n/a
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

Oct 24 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

ra*******@gmail.com wrote:
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

Oct 24 '06 #2

P: n/a
ra*******@gmail.com wrote:
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/~seande...ReverseObvious

and lots of other results.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Oct 24 '06 #3

P: n/a
2006-10-24 <11**********************@e3g2000cwe.googlegroups. com>,
ra*******@gmail.com wrote:
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;
Oct 24 '06 #4

P: n/a
ra*******@gmail.com wrote:
>
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;
}

--
pete
Oct 24 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.