473,766 Members | 2,060 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 #1
47 16564

"Kapil Khosla" <kh*********@ya hoo.com> wrote in message
news:91******** *************** ***@posting.goo gle.com...
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


Create a temp Byte and AND the original byte with mask 0x1 to see if there
is a 1 in the LSB.
If there is a 1 then OR 0x80 to the temp byte

Now AND the original with 0x2 and if its a 1 then OR the temp with 0x4

And so on.

HTH
Allan
Nov 13 '05 #2
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.

Nov 13 '05 #3
Kevin Easton wrote:
Kapil Khosla <kh*********@ya hoo.com> wrote: ....
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.


http://graphics.stanford.edu/~seander/bithacks.html
The easiest, clearest and most efficient way is just to use a 256-entry
lookup table.


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?

Jirka

Nov 13 '05 #4

"Kapil Khosla" <kh*********@ya hoo.com> schrieb im Newsbeitrag
news:91******** *************** ***@posting.goo gle.com...
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.


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++)
{
in = (unsigned char)j;
printf("in %x", (unsigned)in);
for(i = 0, out = 0; i < CHAR_BIT; i++)
{
in >>= (i != 0);
out <<= (i != 0);
out = (unsigned char)(out | (in & 1));
}
out = (unsigned char)(out | (in & 1));
printf(" out %x\n", (unsigned)out);
}
}

Critique and improvrment suggestions welcome :)
Especially about the casts...
Robert
Nov 13 '05 #5

"Robert Stankowic" <pc******@netwa y.at> schrieb im Newsbeitrag
news:3f******** *************** @newsreader01.h ighway.telekom. at...

"Kapil Khosla" <kh*********@ya hoo.com> schrieb im Newsbeitrag
news:91******** *************** ***@posting.goo gle.com...
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.

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++)
{
in = (unsigned char)j;
printf("in %x", (unsigned)in);
for(i = 0, out = 0; i < CHAR_BIT; i++)
{
in >>= (i != 0);
out <<= (i != 0);
out = (unsigned char)(out | (in & 1));
}
out = (unsigned char)(out | (in & 1));


Oops, drop the statement above - sorry
printf(" out %x\n", (unsigned)out);
}
}

Critique and improvrment suggestions welcome :)
Especially about the casts...
Robert

Nov 13 '05 #6
Allan Bruce wrote:

"Kapil Khosla" <kh*********@ya hoo.com> wrote in message
news:91******** *************** ***@posting.goo gle.com...
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


Create a temp Byte and AND the original byte
with mask 0x1 to see if there is a 1 in the LSB.
If there is a 1 then OR 0x80 to the temp byte


You don't start with 0x80 on clc.

step 1
Use unsigned char for mask variables.
One portable expression for the initial value of the high mask is:
(((unsigned char)-1 >> 1) + 1)
As stated, 1 is the initial value for the low mask.

step 2
Do a bitwise AND of the high mask and the byte, and
a bitwise AND of the low mask and the byte.

step 3
If the results of the bitwise ANDs are not either both zero
or both nonzero, then the values of the bits being tested
are different and they should be flipped.
One way to do that, would be to XOR the value of the byte
with the result of a bitwise OR of the two masks,
and change the value of the byte to the XOR result.

step 4
Change the values of the high and low mask.
Shift the high mask right by 1 and shift the low mask left by 1.

step 5
If the high mask is greater than the low mask,
then repeat, starting at step 2.
Nov 13 '05 #7
Robert Stankowic wrote:
....
unsigned char out; .... out = (unsigned char)(out | (in & 1));


Oops, drop the statement above - sorry


And drop the cast, too. The result of (out | (in & 1)) is assigned
to an an unsigned char. The cast is redundant.

Jirka

Nov 13 '05 #8
Robert Stankowic wrote:

"Robert Stankowic" <pc******@netwa y.at> schrieb im Newsbeitrag
news:3f******** *************** @newsreader01.h ighway.telekom. at...

"Kapil Khosla" <kh*********@ya hoo.com> schrieb im Newsbeitrag
news:91******** *************** ***@posting.goo gle.com...
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.


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++)
{
in = (unsigned char)j;
printf("in %x", (unsigned)in);
for(i = 0, out = 0; i < CHAR_BIT; i++)
{
in >>= (i != 0);
out <<= (i != 0);
out = (unsigned char)(out | (in & 1));
}
out = (unsigned char)(out | (in & 1));


Oops, drop the statement above - sorry
printf(" out %x\n", (unsigned)out);
}
}

Critique and improvrment suggestions welcome :)
Especially about the casts...


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

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

j = 0;
while (++j != 0) {
in = j;
printf("in %x", in);
for (i = 0, out = 0; i < CHAR_BIT; i++) {
in >>= (i != 0);
out <<= (i != 0);
out |= in & 1;
}
printf(" out %x\n", out);
}
return 0;
}
Nov 13 '05 #9
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?
--
Joe Wright mailto:jo****** **@earthlink.ne t
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #10

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

Similar topics

35
3745
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
24776
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
33071
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
15642
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
19798
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
6834
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
10168
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
10008
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
9959
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
8833
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
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
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
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.