A
Ajay
Hi all,can you please tell the most efficient method to reverse a
byte.Function should return a byte that is reversed.
byte.Function should return a byte that is reversed.
Ajay said:Hi all,can you please tell the most efficient method to reverse a
byte.Function should return a byte that is reversed.
Ajay said:Hi all,can you please tell the most efficient method to reverse a
byte.Function should return a byte that is reversed.
---8<---- sniped totally wrong lookup table ---8<----Charles said:Ajay said:Hi all,can you please tell the most efficient method to reverse a
byte.Function should return a byte that is reversed.
Use a look up table (untested generated code below):
static unsigned char
reverse_byte(unsigned char b)
{
static const unsigned char b_tbl[] = {
};
return b_tbl;
}
-Charlie
---8<---- sniped totally wrong lookup table ---8<----Charles said:Ajay said:Hi all,can you please tell the most efficient method to reverse a
byte.Function should return a byte that is reversed.
Use a look up table (untested generated code below):
static unsigned char
reverse_byte(unsigned char b)
{
static const unsigned char b_tbl[] = {
};
return b_tbl;
}
-Charlie
Charles said:---8<---- sniped totally wrong lookup table ---8<----Charles said:Ajay wrote:Hi all,can you please tell the most efficient method to reverse
a byte.Function should return a byte that is reversed.
Use a look up table (untested generated code below):
static unsigned char
reverse_byte(unsigned char b)
{
static const unsigned char b_tbl[] = {};
return b_tbl;
}
probably want something like this:
static const unsigned char b_tbl[] = {
0x0, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
...,
0xf, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
};
you can fill in the blanks.
Hi
I give here a piece of code to reverse a byte but iam not sure how good
it is...
unsigned int n=0x01;
unsinged int c=0x00;
int i;
/* b is given byte */
for( i=0; i<8 ;i++)
{
if( b & n) { c=c + 1;}
c<<1;
n<<1;
}
/* the reversed value is stored in c */
any one please correct this if any wrong.
Ajay said:Hi all,can you please tell the most efficient method to reverse a
byte.Function should return a byte that is reversed.
sudharsan said:I give here a piece of code to reverse a byte but iam not sure how good
it is...
unsigned int n=0x01;
unsinged int c=0x00;
int i;
/* b is given byte */
#endiffor( i=0; i<8 ;i++)
{
if( b & n) { c=c + 1;}
c<<1;
n<<1;
}
/* the reversed value is stored in c */
Ajay said:Hi all,can you please tell the most efficient method to reverse a
byte.Function should return a byte that is reversed.
Ajay said:Hi Eric,Firsto of all
said:thanks for the solution.I understand here what
you want to say but i think in any language(specially in C
language)implementation of algorithm is the most importent thing.this
is not about a simple question like this.But the habbit to think
differently start from this point.All the big companies i have gone for
ask only for efficient and fast solution(with respect to
memory/speed).Anybody can make program..isn't it?bt it depend how you
make it.
I appreciate for your solution but i want to impement the program on my
own.See if you can help me out---
Function prototype shuold be Byte * Reverse(byte)
where byte for example:--- 11100010
output should be ---------- 01000111
several ways here:Function prototype shuold be Byte * Reverse(byte)
where byte for example:--- 11100010
output should be ---------- 01000111
Whence this unhealthy fascination with "most efficient?"
Two minutes ago you asked for the "most efficient" way to find
a substring, now you're asking for the "most efficient" way
to reverse a byte. What's next? The "most efficient" way to
listen to the Emperor Concerto?
For future reference: The C language says nothing about the
amount of time any operation requires, nothing about the amount
of space any executable code occupies, nothing about the power
consumption of the computer running the code, and very little
about the amount of space taken by data objects. Most notions
of "efficiency" incorporate one or more of these measures, so
questions about "efficiency" are usually unanswerable in terms
of C. They may be answerable for a particular implementation of
C on a particular computer, but in that case you should read
the documentation for that implementation or ask your question
in a forum devoted to that implementation; this is the wrong
forum for such questions.
... and if you'll accept a tiny bit of advice from a battle-
scarred veteran: "efficiency" is not the most important attribute
of a program. For example, here is the most "efficient" method
I can think of to solve your byte-reversal problem:
#define REVERSE_BYTE(x) 0
On most machines this will be faster and shorter than almost any
alternative you can come up with, and sometimes it will even give
the right answer. Use it in good health.
Ajay said:Hi Eric,Firsto of all thanks for the solution.I understand here what
you want to say but i think in any language(specially in C
language)implementation of algorithm is the most importent thing.this
is not about a simple question like this.But the habbit to think
differently start from this point.All the big companies i have gone for
ask only for efficient and fast solution(with respect to
memory/speed).Anybody can make program..isn't it?bt it depend how you
make it.
I appreciate for your solution but i want to impement the program on my
own.See if you can help me out---
Function prototype shuold be Byte * Reverse(byte)
where byte for example:--- 11100010
output should be ---------- 01000111
Keith Thompson said:Your prototype:
Byte *Reverse(byte)
looks odd. There is no predefined type "Byte" or "byte" in C (those
are two different identifiers), and I can't imagine why you'd want to
return a pointer. You probably want
unsigned char reverse(unsigned char *x);
Richard G. Riley said:.... snip ...
No, but it is a language picked for its brevity and efficiency :
a fair programmer can almost see the instructions generated and
frequently pick the language for just that feature. There is a
reason that Java or Perl or somesuch is not used for writing 3d
modelling engines expected to render 32 bit graphics in real time.
Ajay said:Hi all,can you please tell the most efficient method to reverse a
CBFalconer said:Charles said:Charles said:Ajay wrote:Hi all,can you please tell the most efficient method to reverse
a byte.Function should return a byte that is reversed.
Use a look up table (untested generated code below):
static unsigned char
reverse_byte(unsigned char b)
{
static const unsigned char b_tbl[] = {
---8<---- sniped totally wrong lookup table ---8<----
};
return b_tbl;
}
probably want something like this:
static const unsigned char b_tbl[] = {
0x0, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
...,
0xf, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
};
you can fill in the blanks.
Works like a charm, NOT, when CHAR_BIT > 8. i.e. document hidden
assumptions. In order of execution:
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.