473,785 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reversing a byte

Hi all,can you please tell the most efficient method to reverse a
byte.Function should return a byte that is reversed.

Mar 22 '06
45 5231
Ajay wrote:
Hi all,can you please tell the most efficient method to reverse a
byte.Function should return a byte that is reversed.


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.

--
Eric Sosman
es*****@acm-dot-org.invalid
Mar 22 '06 #11
Hi Eric,Firsto of all thanks for the solution.I understand here what
you want to say but i think in any language(specia lly in C
language)implem entation 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

Mar 22 '06 #12

Ajay wrote:
Hi Eric,Firsto of all
Quote context! Read:

<http://cfaj.freeshell. org/google/>
<http://clc-wiki.net/wiki/Introduction_to _comp.lang.c>
thanks for the solution.I understand here what
you want to say but i think in any language(specia lly in C
language)implem entation 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.
The right approach is:

1. code it in a readable and correct fashion
2. *mesure*
3. if 2. is unsatisfactory, only then improve efficiency

If you don't plan on doing 2., the rules of optimisation are:

1. Don't do it
2. (experts only) Don't do it yet.
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


By writing it for you? Fat chance!

--
BR, Vladimir

Mar 22 '06 #13
Ajay, le 22/03/2006 a écrit :
[...]
Function prototype shuold be Byte * Reverse(byte)

where byte for example:--- 11100010
output should be ---------- 01000111

several ways here:
http://graphics.stanford.edu/~seande...ReverseObvious

You can choose calculation methods for short code, lookup table method
for fast code.

You can use your own code for a calculation method generating the
lookup table (text) and copy-paste in your final code.

When you have Reverse(), you check Reverse(Reverse ()) is identity.

--
Pierre Maurette
Mar 22 '06 #14
On 2006-03-22, Eric Sosman <es*****@acm-dot-org.invalid> wrote:
Ajay wrote:
Hi all,can you please tell the most efficient method to reverse a
byte.Function should return a byte that is reversed.
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?


Whereas I agree with your general concensus that an unhealthy
obsession with speed can lead to buggy and convoluted code, it is
also, often the case that the efficient way is also the easiest and
most reliable way in a relatively low level language such as C. The
fact that its a byte being talked about suggests that the reversal is
required in a byte/character orientated arena where efficiency and cpu
cycles do need to be saved. I would not, however, counter sticking
setjmps in or gotos to save a few cycles for non critical function
return replacement : especially if the contents of those functions
were to contain the bulk of the work. Its a % game.

For new programmers, a lot of employers are interested in their
abilities to maximise the performance of a C program while maintaining
readability and maintability. This group can, and does, help in that
education.

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
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.
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.
I dont agree. It is a forum for the discussion of the C Language : and
how to do something is as on topic as the result of that procedure. To
suggest that looking for efficiency is not a good thing with a
language like C is suggesting that aiming for structured classes in
Java or C++ is counterproducti ve to achieving the result.

Efficient code does not have to be nasty code.

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


To the OP : since a byte is only representing (generally..) 8 bits or
256 values then you could look up in an array which you initialise at
program start. Efficient. Else, test bit 0 of source byte, if set then
set bit 0 of destination word. Left shift dest, right shift
source. Repeat 8 times. Look up how to calculate number of bits to be
portable. There may be better ways.

Good luck!

Mar 22 '06 #15
"Ajay" <aj***********@ gmail.com> writes:
Hi Eric,Firsto of all thanks for the solution.I understand here what
you want to say but i think in any language(specia lly in C
language)implem entation 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)
If you want to implement it on your own, what are you asking us for?
(That's a serious question.)

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(unsigne d char *x);
where byte for example:--- 11100010
output should be ---------- 01000111


And please read <http://cfaj.freeshell. org/google/> if you want anyone
to know what you're talking about.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 22 '06 #16
Keith Thompson <ks***@mib.or g> writes:
[...]
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(unsigne d char *x);


Sorry, I *really* didn't want the '*' there. The prototype should be

unsigned char reverse(unsigne d char x);

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 22 '06 #17
"Richard G. Riley" wrote:
On 2006-03-22, Eric Sosman <es*****@acm-dot-org.invalid> wrote:

.... snip ...

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


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.


For a minority, yes. It seems to me that the majority of
programmers today wouldn't recognize a conditional jump, or an
indirect reference, in assembly code if it jumped up and bit them
on the gluteus maximus.

--
"The power of the Executive to cast a man into prison without
formulating any charge known to the law, and particularly to
deny him the judgement of his peers, is in the highest degree
odious and is the foundation of all totalitarian government
whether Nazi or Communist." -- W. Churchill, Nov 21, 1943
Mar 23 '06 #18
Ajay wrote:
Hi all,can you please tell the most efficient method to reverse a
efficient here is compact code or compact logic not other stuffs..
byte.Function should return a byte that is reversed.


Mar 23 '06 #19
CBFalconer wrote:
Charles Mills wrote:
Charles Mills wrote:
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[b];
}


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:


True, but I highly suspect that the OP will ever use an implementation
that defines "CHAR_BIT > 8". Nor would I suspect that most other C
programmers will.

--
jay
Mar 23 '06 #20

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

Similar topics

4
5974
by: Kevin | last post by:
Hello, I need to some help in reversing an 2-dimensional array. I am working with gif images and I am trying to make the mirror image. I was hoping that someone could help give me a headstart in how I can accomplish this. Also, I don't know the the size of the array before hand as the image can be any size. I already have the my read and write gif functions working, but I just need to know how to reverse the contents.
11
8094
by: Tim Marshall | last post by:
I use Terry Kreft's & Stephen Lebans colour dialog procedures for users to pick colours for various control properties in certain apps. Is there a way to take the colour code that is displayed in a backcolor/forecolor/etc property and calculate the "reverse colour"? In other words, If a user picks 255 (red) for a control backcolor, I'd like to be able to calculate the opposite or negative of that colour and assign the control's...
8
4763
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. --------------------------------- PROGRAMME -------------------------------- /* K&R2 section 1.9 exercise 1.19
0
9646
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10157
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
10096
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
8982
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
7504
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
6742
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
5386
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
5514
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3658
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.