reverse byte

J

Jirka Klaue

Default said:
I did even better, posted my results. Why should I post a link?

Who said you should? *You* said, I should google for the look-up table.
And the question is: why? There are two things you obviously missed.
First: A link was already posted. Second: *Copy* the table in my code?
Without testing it? Since testing it requires an algorithm and once I've
written the algorithm in C, I do *not* need to copy the table.
I don't recommend any other search engine.

Could turn out to be a mistake.

Jirka
 
J

Jack Klein

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.

The OP said he wanted to reverse a byte. He said nothing at all about
8-bit values. In C, by definition, a byte has CHAR_BIT bytes, where
CHAR_BIT >= 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.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
R

Robert Stankowic

CBFalconer said:
Robert said:
"Jack Klein" <[email protected]> schrieb im Newsbeitrag
[....]
Shudder ;)

OK
unsigned i;
unsigned j;
Better?

Nope. What if UCHAR_MAX == UINT_MAX.

Sh*t :((
OK, what about

#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 = UCHAR_MAX; j; j--)
{
in = (unsigned char)(UCHAR_MAX - j + 1);
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);
}
return EXIT_SUCCESS;
}

I _hope_ I got it right now.
UINT_MAX can hopefully not be less than UCHAR_MAX
cheers
Robert
 
P

pete

[snip]
That's the only kind of code that's really on topic here.

I'm not sure what you mean.

This group focuses on portable code.

A program which displays the bit reversed values of
all of the possible byte values,
has implementation defined output,
since the width of a byte is implementation defined,
and so is not strictly speaking "a portable program".

However, code which will do what it's supposed to do,
on any system, regardless of what values are in <limits.h>
or any other standard library headers,
seems portable enough to be on topic,
though code which only works when CHAR_BIT equals 8, is off topic.

If you were to ask a question about how to display
the values of each byte in a int variable,
I would expect to see the C keyword "sizeof",
somewhere in the answer, rather than an answer
which assumed what the value of sizeof(int) was.
 
D

Default User

Jirka said:
Who said you should? *You* said, I should google for the look-up table.
And the question is: why? There are two things you obviously missed.
First: A link was already posted.

I didn't follow the link, as you started asking how tables would be
generated. I assumed the bithacks was another bit-manipulation routine.
Second: *Copy* the table in my code?
Without testing it? Since testing it requires an algorithm and once I've
written the algorithm in C, I do *not* need to copy the table.

I was able to do it by examination. The pattern is pretty clear.



Brian Rodenborn
 
M

Mac

Mac said:
Mac wrote:
When I had to solve this problem,
I wanted to support any number of bits.

That's the only kind of code that's really on topic here.

I'm not sure what you mean.

This group focuses on portable code.

Understood.

A program which displays the bit reversed values of
all of the possible byte values,
has implementation defined output,
since the width of a byte is implementation defined,
and so is not strictly speaking "a portable program".

However, code which will do what it's supposed to do,
on any system, regardless of what values are in <limits.h>
or any other standard library headers,
seems portable enough to be on topic,
though code which only works when CHAR_BIT equals 8, is off topic.

I see what you mean.
If you were to ask a question about how to display
the values of each byte in a int variable,
I would expect to see the C keyword "sizeof",
somewhere in the answer, rather than an answer
which assumed what the value of sizeof(int) was.

Me too. ;-)

regards,
Mac
--
 
K

Kevin Easton

Jack Klein said:
The OP said he wanted to reverse a byte. He said nothing at all about
8-bit values. In C, by definition, a byte has CHAR_BIT bytes, where
CHAR_BIT >= 8.

The examples, still visisble in the quoted text, had binary values
zero-padded out to 8 bits. Why stop the zero padding at 8 bits if
you're interested in more than 8 bits? That was my reasoning, anyway -
I did consider the issue.

- Kevin.
 
Joined
May 6, 2008
Messages
1
Reaction score
0
Code:
#define mirror_byte(byte) do{\
                                (byte)=((((byte)<<7)&0x80)|(((byte)>>7)&0x01))|((byte)&0x7E);\
                                (byte)=((((byte)<<5)&0x40)|(((byte)>>5)&0x02))|((byte)&0xBD);\
                                (byte)=((((byte)<<3)&0x20)|(((byte)>>3)&0x04))|((byte)&0xDB);\
                                (byte)=((((byte)<<1)&0x10)|(((byte)>>1)&0x08))|((byte)&0xE7);\
                            }while(0)

this is how i reverse a byte, parameter should not have any side effects etc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top