473,890 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reverse byte

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 about it.

I know basic bit manipulation , shifting left, right and have done
simple problems like counting 1's etc but this one doesnt seem to
click to me.

Thanks.
Kapil
Nov 13 '05
47 16602
On Fri, 26 Sep 2003 11:17:38 +0000, Kevin Easton wrote:
The easiest, clearest and most efficient way is just to use a 256-entry
lookup table.


Forgive me for being architecture specific but intel chips have ror and rol
(roll bits right / left) instructions don't they? If the target processor
has similar instructions that would be far more effictient than any lookup
table or bit manipulation presented already.

Z.

Nov 13 '05 #21
Kapil Khosla <kh*********@ya hoo.com> wrote:
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 about it. I know basic bit manipulation , shifting left, right and have done
simple problems like counting 1's etc but this one doesnt seem to
click to me.

This should do it:

#include <stdio.h>
#include <limits.h>

void display_binary( unsigned char val)
{
int i;

for(i = CHAR_BIT - 1; i >= 0; i--)
printf("%d", (val >> i) & 0x01);

printf("\n");
}

int main(void)
{
unsigned char input = 0xD0;
unsigned char output = 0;
unsigned char cur;
int i;

printf("Before: ");
display_binary( input);

for(i = 0; i < CHAR_BIT; i++)
{
cur = (input >> i) & 0x01;
output |= (cur << CHAR_BIT - i - 1);
}

printf("After: ");
display_binary( output);

return 0;
}

Alex
Nov 13 '05 #22

"Christophe r Benson-Manica" <at***@nospam.c yberspace.org> wrote in message
news:bl******** **@chessie.cirr .com...
Jirka Klaue <jk****@ee.tu-berlin.de> spoke thus:
That might be true. But it's not very helpful.
How is the lookup table generated? You wouldn't do this by hand, would
you?
How's this? (note it only goes forward - the reverse one is nearly the

same)

ummm, isn't the reverse table exactly the same?

--
Roger
Nov 13 '05 #23

"Zygmunt Krynicki" <zyga@_CUT_2zyg a.MEdyndns._OUT _org> wrote in message
news:pan.2003.0 9.26.19.03.19.1 55155@_CUT_2zyg a.MEdyndns._OUT _org...
On Fri, 26 Sep 2003 11:17:38 +0000, Kevin Easton wrote:
The easiest, clearest and most efficient way is just to use a 256-entry
lookup table.
Forgive me for being architecture specific but intel chips have ror and

rol (roll bits right / left) instructions don't they? If the target processor
has similar instructions that would be far more effictient than any lookup
table or bit manipulation presented already.

Z.


If we wanted to rotate bits, yes; but the question was how to reverse
bits...

--
Roger
Nov 13 '05 #24
Jack Klein <ja*******@spam cop.net> wrote:
On Fri, 26 Sep 2003 11:17:38 GMT, Kevin Easton
<kevin@-nospam-pcug.org.au> wrote in comp.lang.c:
Kapil Khosla <kh*********@ya hoo.com> wrote:
> 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 about it.


The easiest, clearest and most efficient way is just to use a 256-entry
lookup table.

- Kevin.


On the Texas Instruments 2812 that I am writing code for today, it
would require 65,536 entries, because CHAR_BIT is 16, so that's how
many bits a byte has.

On the Analog Devices SHARC I coded for a few years ago, that would
have required a 4GB x 32 bit table, since all the integer types were
32 bits wide.

But I agree, except in very tight memory situations, a look up table
is by far the fastest if CHAR_BIT is 8.


I think the OPs example made it clear that he wanted to reverse an 8 bit
value (due to the zero padding stopping at the 8th bit), rather than
reverse an arbitrary char value.

- Kevin.

Nov 13 '05 #25
Zygmunt Krynicki <zyga@_cut_2zyg a.medyndns._out _org> wrote:
On Fri, 26 Sep 2003 11:17:38 +0000, Kevin Easton wrote:
The easiest, clearest and most efficient way is just to use a 256-entry
lookup table.


Forgive me for being architecture specific but intel chips have ror and rol
(roll bits right / left) instructions don't they? If the target processor
has similar instructions that would be far more effictient than any lookup
table or bit manipulation presented already.


I don't see how rotate instructions help you implement the requested
transformation.

- Kevin.

Nov 13 '05 #26
Robert Stankowic wrote:
"Jack Klein" <ja*******@spam cop.net> schrieb im Newsbeitrag
On Fri, 26 Sep 2003 13:58:29 +0200, "Robert Stankowic"
"Kapil Khosla" <kh*********@ya hoo.com> schrieb im Newsbeitrag
>
> 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 about it.
>
> I know basic bit manipulation , shifting left, right and
> have done simple problems like counting 1's etc but this one
> doesnt seem to click to me.
>

OK, fireproof suit and helmet on....

#include <stdlib.h>
#include <limits.h>
#include <stdio.h>

int main(void)
{
unsigned char in = 0;
unsigned char out;
int i;
int j;

for(j = 1; j <= UCHAR_MAX ; j++)


Not all that common (except to embedded programmers like me), but
there is the distinct possibility that UCHAR_MAX is > INT_MAX.
Infinite loop.


Shudder ;)

OK
unsigned i;
unsigned j;
Better?


Nope. What if UCHAR_MAX == UINT_MAX.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 13 '05 #27
Roger Willcocks wrote:
"Zygmunt Krynicki" <zyga@_CUT_2zyg a.MEdyndns._OUT _org> wrote
On Fri, 26 Sep 2003 11:17:38 +0000, Kevin Easton wrote:
The easiest, clearest and most efficient way is just to use
a 256-entry lookup table.


Forgive me for being architecture specific but intel chips
have ror and rol (roll bits right / left) instructions don't
they? If the target processor has similar instructions that
would be far more effictient than any lookup table or bit
manipulation presented already.


If we wanted to rotate bits, yes; but the question was how to
reverse bits...


You are allowed to rotate bits left into the carry, and then right
from the carry into the destination. However no such operations
are built into C.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!

Nov 13 '05 #28
Mac
On Fri, 26 Sep 2003 13:20:27 +0000, Joe Wright wrote:
Kapil Khosla wrote:

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 about it.

I know basic bit manipulation , shifting left, right and have done
simple problems like counting 1's etc but this one doesnt seem to
click to me.

Thanks.
Kapil


int revb(int n) {
n = ((n >> 1) & 0x55) | ((n << 1) & 0xaa);
n = ((n >> 2) & 0x33) | ((n << 2) & 0xcc);
n = ((n >> 4) & 0x0f) | ((n << 4) & 0xf0);
return n;
}

click?

Clicks for me.

This is really a beautiful solution. The flow of bits from one stage to
the next is just like the flow of data through the classic radix 2 FFT,
which, interestingly enough, is where the problem of reversing bits seems
to be used most of the time.

Too bad it only works when the number of bits to reverse is a power of two.

Mac
--
Nov 13 '05 #29
Roger Willcocks <rk**@rops.or g> spoke thus:
ummm, isn't the reverse table exactly the same?


Well, if the idea is to reverse bytes (and generate a lookup table), table[1]
should be 11111110 (assuming 8-bit characters), right?

--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cybers pace.org |
Nov 13 '05 #30

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

Similar topics

35
3770
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 ------------------------------------------------------------- PEP: 323
6
1306
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 = 1; aWriter.WriteInt32(someValue); the byte stream that appears in the file is 01 00 00 00
8
675
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 and get the source nicety, although there have comeout with some "obfuscators" solution, but the structure still remain exactly same as the source after obfuscate. me unable to understand what the benefit will IL bring to me, is that benefit of...
6
24780
by: John Deuf | last post by:
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.
20
33105
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. From here read a character, print it, move the file pointer (FILE*) to 2 steps back (using fseek(fp, -2, SEEK_CUR)) to read the previous character. This seems to be ok if the file has a single line (i.e. no new line character). The above logic...
3
15660
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 and then call Array.Reverse(), but how to convert back to byte? How about flipping an odd number of bits, say a group of 4 bits, then converting the 4 bits back to an int equivalent? Thanks you in advance for your help,
20
19823
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
6849
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 will give the reversed char. e.g., unsigned char x = 0x8A am expecting an output after reversal x = 0x51
0
11234
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10829
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10925
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9640
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6058
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4682
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4276
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3282
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.