reversing a byte

C

CBFalconer

pete said:
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.com, 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/googlegroupsreply/>
 
K

Keith Thompson

CBFalconer said:
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.
 
R

Richard Heathfield

jaysome said:
Richard said:
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.
 
C

Charles Krug

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

jaysome

Richard said:
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):#define CHAR_BIT 8 /* bits per char */

Atmel:
"grep" C:\WINAVR

limits.h(6):#define CHAR_BIT __CHAR_BIT__

__CHAR_BIT__ = 8

Between Microchip and Atmel, that's a lot of chips.
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.
 
V

Vladimir S. Oka

jaysome opined:
said:
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.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top