473,785 Members | 2,255 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 5229
pete wrote:
pete wrote:
Pedro Graca wrote:

hi_mask = (UCHAR_MAX + 1) >> 1;

is more portable?


Not more portable, but slightly better style, perhaps.


Actually, (UCHAR_MAX + 1) is not portable.

If UCHAR_MAX is equal to UINT_MAX, then
(hi_mask = (UCHAR_MAX + 1) >> 1)
is equal to zero.


and, if UCHAR_MAX is equal to INT_MAX, the expression creates
undefined (or system defined) behaviour, due to the silly way that
integer promotions work. Unsignedness is not preserved.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>
Also see <http://www.safalra.com/special/googlegroupsrep ly/>
Mar 24 '06 #41
CBFalconer <cb********@yah oo.com> writes:
pete wrote:
pete wrote:
Pedro Graca wrote:

hi_mask = (UCHAR_MAX + 1) >> 1;

is more portable?

Not more portable, but slightly better style, perhaps.


Actually, (UCHAR_MAX + 1) is not portable.

If UCHAR_MAX is equal to UINT_MAX, then
(hi_mask = (UCHAR_MAX + 1) >> 1)
is equal to zero.


and, if UCHAR_MAX is equal to INT_MAX, the expression creates
undefined (or system defined) behaviour, due to the silly way that
integer promotions work. Unsignedness is not preserved.


It's unlikely that any real implementation would have UCHAR_MAX ==
INT_MAX. CHAR_BIT would have to be at least 15, and int would have to
have exactly CHAR_BIT value bits (plus 1 sign bit), and therefore at
least CHAR_BIT-1 padding bits (since the total number of bits must be
a multiple of CHAR_BIT). Conforming, but silly.

--
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 24 '06 #42
jaysome said:
Richard Heathfield wrote:
jaysome said:
Nor would I suspect that most other C programmers will [ever use a
system with CHAR_BIT > 8].

Well, I certainly have used such an implementation, and so have a whole
bunch of people I was working with at the time, and so has anyone else
who has written C for the same chip, and so have lots of other people
writing C for other similar chips, too. And such chips, common a few
years ago, are becoming yet more common all the time.


Okay.

But the fact that you and your co-workers and probably some of your
friends and their friends have used such an implementation does not
hardly qualify for "most" C programmers.


Sure, but the kind of chips we're talking about are the very stuff of which
embedded systems are made, and that's where most C work is being done. I
would not be even remotely surprised if CHAR_BIT > 8 systems actually
outnumbered CHAR_BIT = 8 systems.
To be sure--a table impementation is fine as long as you understand the
ramifications to portability.
Absolutely.
And I reiterate--most C programmers will
be just fine, whether of not they understand the ramifications.


Provided they stay away from the Real World, I agree entirely.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Mar 24 '06 #43
On 2006-03-24, Keith Thompson <ks***@mib.or g> wrote:
CBFalconer <cb********@yah oo.com> writes:
pete wrote:
pete wrote:
Pedro Graca wrote:
>
> hi_mask = (UCHAR_MAX + 1) >> 1;
>
> is more portable?

Not more portable, but slightly better style, perhaps.

Actually, (UCHAR_MAX + 1) is not portable.

If UCHAR_MAX is equal to UINT_MAX, then
(hi_mask = (UCHAR_MAX + 1) >> 1)
is equal to zero.


and, if UCHAR_MAX is equal to INT_MAX, the expression creates
undefined (or system defined) behaviour, due to the silly way that
integer promotions work. Unsignedness is not preserved.


It's unlikely that any real implementation would have UCHAR_MAX ==
INT_MAX. CHAR_BIT would have to be at least 15, and int would have to
have exactly CHAR_BIT value bits (plus 1 sign bit), and therefore at
least CHAR_BIT-1 padding bits (since the total number of bits must be
a multiple of CHAR_BIT). Conforming, but silly.


I'm not sure K&R gave any thought to odd ducks like this, any more than
the case where single bits were addressable like the old (iirc)
Borroughs machines.

Mar 25 '06 #44
Richard Heathfield wrote:
jaysome said:

Richard Heathfield wrote:

jaysome said:
Nor would I suspect that most other C programmers will [ever use a
system with CHAR_BIT > 8].
Well, I certainly have used such an implementation, and so have a whole
bunch of people I was working with at the time, and so has anyone else
who has written C for the same chip, and so have lots of other people
writing C for other similar chips, too. And such chips, common a few
years ago, are becoming yet more common all the time.


Okay.

But the fact that you and your co-workers and probably some of your
friends and their friends have used such an implementation does not
hardly qualify for "most" C programmers.

Sure, but the kind of chips we're talking about are the very stuff of which
embedded systems are made, and that's where most C work is being done. I
would not be even remotely surprised if CHAR_BIT > 8 systems actually
outnumbered CHAR_BIT = 8 systems.


I'd be very surprised.

Microchip:
"grep" C:\HTSOFT\PIC18 \include

limits.h(3):#de fine CHAR_BIT 8 /* bits per char */

Atmel:
"grep" C:\WINAVR

limits.h(6):#de fine CHAR_BIT __CHAR_BIT__

__CHAR_BIT__ = 8

Between Microchip and Atmel, that's a lot of chips.
And I reiterate--most C programmers will
be just fine, whether of not they understand the ramifications.

Provided they stay away from the Real World, I agree entirely.


And I reiterate--most C programmers will be just fine.

When, and if, they ever get to develop with an implementation that
defines CHAR_BIT > 8, and that makes a difference compared to CHAR_BIT =
8, "most" C programmers will directly or indirectly (e.g., via the
testing group) force a result that leads them to conclude that their
assumption about CHAR_BIT = 8 is invalid; subsequently, they would
follow up with a resolution.

That's my Real World, anyways. YMMV.

--
jay
Mar 25 '06 #45
jaysome opined:
Richard Heathfield wrote:
jaysome said:
Richard Heathfield wrote:
jaysome said:
<snip>
And I reiterate--most C programmers will
be just fine, whether of not they understand the ramifications.
Provided they stay away from the Real World, I agree entirely.


And I reiterate--most C programmers will be just fine.

When, and if, they ever get to develop with an implementation that

^^^^^^^

What about "port old code"?
defines CHAR_BIT > 8, and that makes a difference compared to
CHAR_BIT = 8, "most" C programmers will directly or indirectly (e.g.,
via the testing group) force a result that leads them to conclude
that their assumption about CHAR_BIT = 8 is invalid; subsequently,
they would follow up with a resolution.
Which, in case of "maintain old code" leads to endless hunt for all the
places where integer constant 8 needs to be changed to 13.
That's my Real World, anyways. YMMV.


Not all people are lucky enough to have it easy, and do only
green-field development.

--
BR, Vladimir

He knows not how to know who knows not also how to unknow.
-- Sir Richard Burton

Mar 25 '06 #46

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
10155
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
10095
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
8979
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
7502
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
6741
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
5383
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
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
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.