473,900 Members | 4,979 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 16603
pete wrote:

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 = 1;
do {
in = j;
printf("in %x", in);
i = 1;
out = in & 1;
do {
in >>= 1;
out <<= 1;
out |= in & 1;
++i;
} while (CHAR_BIT > i);
printf(" out %x\n", out);
} while (++j != 0);
return 0;
}
Nov 13 '05 #11

"Jirka Klaue" <jk****@ee.tu-berlin.de> schrieb im Newsbeitrag
news:3F******** ******@ee.tu-berlin.de...
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.


Aren't integer promotions coming into play here?
At least my compiler warns about assigning an int to a char..

Robert
Nov 13 '05 #12
Robert Stankowic wrote:
"Jirka Klaue" schrieb:
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.


Aren't integer promotions coming into play here?
At least my compiler warns about assigning an int to a char..


That's the only reason for the cast: Tell the compiler to stop whining.

Jirka

Nov 13 '05 #13

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

"Jirka Klaue" <jk****@ee.tu-berlin.de> schrieb im Newsbeitrag
news:3F******** ******@ee.tu-berlin.de...
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.


Aren't integer promotions coming into play here?
At least my compiler warns about assigning an int to a char..


Oviously not. Blind spot, sorry.
Robert
Nov 13 '05 #14
Thanks a lot !
I figured out using that hint.

Kapil

"Allan Bruce" <al*****@TAKEAW AYf2s.com> wrote in message news:<bl******* ***@news.freedo m2surf.net>...
"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 #15
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #16
On Fri, 26 Sep 2003 13:58:29 +0200, "Robert Stankowic"
<pc******@netwa y.at> wrote in comp.lang.c:

"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++)


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

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #17
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)

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

char buf[CHAR_BIT+1];

void get_binary( int n )
{
int i;

for( i=0 ; i < CHAR_BIT ; i++ ) {
if( n % 2 ) {
buf[CHAR_BIT-i-1]='1';
}
else {
buf[CHAR_BIT-i-1]='0';
}
n/=2;
}
}

int main(void)
{
const int size=pow( 2, CHAR_BIT );
char **table;
int i;

buf[CHAR_BIT]=0;
printf( "%s\n", buf );
/* Error checking on malloc() omitted */
table=malloc( size * sizeof(char *) );
for( i=0 ; i < size ; i++ ) {
table[i]=malloc( (1+CHAR_BIT) * sizeof(char) );
get_binary( i );
strcpy( table[i], buf );
}

for( i=0; i < size ; i++ ) {
printf( "%s\n", table[i] );
}

return EXIT_SUCCESS;
}

--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cybers pace.org |
Nov 13 '05 #18
Jirka Klaue wrote:
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?

Do what I did, google for one, copy it into your code, presto. Here's
mine:
#define REVERSEBITS(b) (BitReverseTabl e[b])

unsigned char BitReverseTable[256] =
{
0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
0x14, 0x94, 0x54, 0xd4,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7, 0x27, 0xa7, 0x67, 0xe7,
0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
};

Brian Rodenborn
Nov 13 '05 #19

"Jack Klein" <ja*******@spam cop.net> schrieb im Newsbeitrag
news:rc******** *************** *********@4ax.c om...
On Fri, 26 Sep 2003 13:58:29 +0200, "Robert Stankowic"
<pc******@netwa y.at> wrote in comp.lang.c:

"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++)


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?

Thanks
kind regards
Robert
Nov 13 '05 #20

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
33112
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
15662
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
6850
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
9845
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11276
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...
1
10976
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
10497
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
9671
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...
1
8043
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5891
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
6082
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3320
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.