473,385 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Reverse a byte

Does somebody has a better algorithm than mine to reverse a byte
(i.e. bit 0 becomes 7, bit 1 becomes 6 ...)

unsigned char u=0, c = TESTVALUE;
int i;

for (i=0 ; i<4 ; i++) u |= ((c & (1 << i)) << (7-2*i))
| ((c & (1 << 7-i)) >> (7-2*i));

I need something quick. Thx.
Nov 13 '05 #1
6 24747
John Deuf wrote:

Does somebody has a better algorithm than mine to reverse a byte
(i.e. bit 0 becomes 7, bit 1 becomes 6 ...)

unsigned char u=0, c = TESTVALUE;
int i;

for (i=0 ; i<4 ; i++) u |= ((c & (1 << i)) << (7-2*i))
| ((c & (1 << 7-i)) >> (7-2*i));


#include <limits.h>
#if UCHAR_MAX != 0xFF
#error "Re-build table for non-8-bit byte!"
#endif
...
static const unsigned char reversed[] = {
0x00, 0x80, 0x40, 0xC0, ..., 0xEF, oxFF };
u = reversed[c];

--
Er*********@sun.com
Nov 13 '05 #2
John Deuf wrote:
Does somebody has a better algorithm than mine to reverse a byte
(i.e. bit 0 becomes 7, bit 1 becomes 6 ...)

unsigned char u=0, c = TESTVALUE;
int i;

for (i=0 ; i<4 ; i++) u |= ((c & (1 << i)) << (7-2*i))
| ((c & (1 << 7-i)) >> (7-2*i));


If your bytes are only 8-bits wide, then I believe a lookup table
will be the fastest solution (as Eric pointed out).

If your bytes are wider, it might not be practical.

Nov 13 '05 #3
"John Deuf" <go****@spongers.com> wrote in message
news:97************************@posting.google.com ...
| Does somebody has a better algorithm than mine to reverse a byte
| (i.e. bit 0 becomes 7, bit 1 becomes 6 ...)
|
| unsigned char u=0, c = TESTVALUE;
| int i;
|
| for (i=0 ; i<4 ; i++) u |= ((c & (1 << i)) << (7-2*i))
| | ((c & (1 << 7-i)) >> (7-2*i));

As an alternative to a lookup, it is possible to perform
the operation in O(lg(N)) instead of O(N):

For an 8-bit byte stored in c:

c = ((c>>1)&0x55)|((c<<1)&0xAA);
c = ((c>>2)&0x33)|((c<<2)&0xCC);
c = (c>>4) | (c<<4) ; //NB: code not tested

This is obviously easy to scale up to 16/32/64 bytes and,
in some situations, could be faster than a lookup...
Regards,
Ivan
--
http://ivan.vecerina.com
Nov 13 '05 #4
John Deuf wrote:
Does somebody has a better algorithm than mine to reverse a byte
(i.e. bit 0 becomes 7, bit 1 becomes 6 ...)

unsigned char u=0, c = TESTVALUE;
int i;

for (i=0 ; i<4 ; i++) u |= ((c & (1 << i)) << (7-2*i))
| ((c & (1 << 7-i)) >> (7-2*i));

I need something quick. Thx.


A better solution may be to use assembly language.
Use the rotation instructions. Rotate the original
right into carry, then rotate carry into the result.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 13 '05 #5
In <bo**********@news-rocq.inria.fr> Grumble <in*****@kma.eu.org> writes:
John Deuf wrote:
Does somebody has a better algorithm than mine to reverse a byte
(i.e. bit 0 becomes 7, bit 1 becomes 6 ...)

unsigned char u=0, c = TESTVALUE;
int i;

for (i=0 ; i<4 ; i++) u |= ((c & (1 << i)) << (7-2*i))
| ((c & (1 << 7-i)) >> (7-2*i));


If your bytes are only 8-bits wide, then I believe a lookup table
will be the fastest solution (as Eric pointed out).

If your bytes are wider, it might not be practical.


For larger values of CHAR_BIT that are powers of two, an adaptation of
the following algorithm is much better than the bit by bit approach:

c = (c & 0x0F) << 4 | (c & 0xF0) >> 4;
c = (c & 0x33) << 2 | (c & 0xCC) >> 2;
c = (c & 0x55) << 1 | (c & 0xAA) >> 1;

For a 16-bit byte you only need one more "iteration", which is left as
an exercise to the reader.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #6
Thanks all, I got what I need here.

This forum is really effective !
Eric Sosman <Er*********@sun.com> wrote in message news:<3F***************@sun.com>...
John Deuf wrote:

Does somebody has a better algorithm than mine to reverse a byte
(i.e. bit 0 becomes 7, bit 1 becomes 6 ...)

unsigned char u=0, c = TESTVALUE;
int i;

for (i=0 ; i<4 ; i++) u |= ((c & (1 << i)) << (7-2*i))
| ((c & (1 << 7-i)) >> (7-2*i));


#include <limits.h>
#if UCHAR_MAX != 0xFF
#error "Re-build table for non-8-bit byte!"
#endif
...
static const unsigned char reversed[] = {
0x00, 0x80, 0x40, 0xC0, ..., 0xEF, oxFF };
u = reversed[c];

Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Raymond Hettinger | last post by:
Here is a discussion draft of a potential PEP. The ideas grew out of the discussion on pep-284. Comments are invited. Dart throwing is optional. Raymond Hettinger ...
6
by: PHil Coveney | last post by:
Hello, I need to support a file format that stores integers with the MSB first and the LSB last. When I use BinaryWriter aWriter = new BinaryWriter(aFileStream, Encoding.ASCII); int someValue...
8
by: xiao zhang yu | last post by:
me was sorry if this question are present before DotNet, no matter VB.Net or C# all they are compiled to IL, and yes, that IL will totally same as "open-sourse", every IL will easy to decompile...
47
by: Kapil Khosla | last post by:
Hi, I am trying to reverse a byte eg. 11010000 should look like 00001011 Plz note, it is not a homework problem and I do not need the c code for it. Just give me an idea how should I proceed...
20
by: sahukar praveen | last post by:
Hello, I have a question. I try to print a ascii file in reverse order( bottom-top). Here is the logic. 1. Go to the botton of the file fseek(). move one character back to avoid the EOF. 2....
3
by: Laszlo Szijarto | last post by:
In C#, wha't the best way to reveser the bit order of a data type and then convert it back to that datatype? So, take a byte, reverse bits, convert it back to a byte. I tried to get a BitArray...
20
by: mike7411 | last post by:
Is there any easy way to reverse the order of the bits in a byte in C++? (i.e. 00000001 becomes 10000000)
4
by: ranjanmg1 | last post by:
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.