Re: How to extract bytes from long?

From: Arthur J. O'Dwyer (ajo_at_nospam.andrew.cmu.edu)
Date: 10/17/03


Date: Fri, 17 Oct 2003 03:05:17 -0400 (EDT)


On Fri, 16 Oct 2003, Samuel Barber wrote:
>
> pete wrote in message news:<3F8F5775.27E1@mindspring.com>...
> > Samuel Barber wrote:
> > > (Peter Nilsson) wrote...
> > > > Samuel Barber wrote...
> > > > > pete wrote in message news:<3F8D3896.363F@mindspring.com>...
> > > > > >
> > > > > > printf("%#x\n", value & (unsigned char)-1);
> > > > >
> > > > > I would use ~0 for "all 1s" rather than -1.
> > > >
> > > > That risks a trap representation under C99.
> > >
> > > Nonsense. ~0 is idiomatic C. Aren't you worried about the "risk" that
> > > -1 may not be implemented as all-1s? That is, after all, an
> > > implementation detail. It's not true for sign-magnitude or 1's
> > > complement, for example.
> >
> > You're wrong about everything.
> >
> > ~0 is negative zero in ones complement.
> > Implementations are allowed trap negative zero.
>
> You're just repeating words, without any understanding. There's no
> such thing as "negative zero" (or negative anything) in the context of
> bitwise operations.

It is AFAIK implementation-defined whether the concept of "negative
zero" is meaningful in standard C (depending on the representation
of signed integers).

> How can a bitwise operation trap? It can't.

Of course it can! (Why wouldn't it? And do modern digital computers
perform any operations that *aren't* bitwise, anyway?)

> > -1 is a value, not a bit pattern.
> > The value of negative one, cast to unsigned char, is UCHAR_MAX.

To clarify pete's point:

 (uchar)-1 == (uchar)-((int)1) == (-(int)1)+UCHAR_MAX == UCHAR_MAX-1

...which is guaranteed to have an all-ones bit pattern, regardless
of padding or n's-complement.

> Hello? The point is that -1 is ***being used as*** a bit pattern.

Then it's being used incorrectly. -1 is *not* a bit pattern, it's
an integer expression equal to the additive inverse of the integer 1.
It doesn't have a "bit pattern" per se.

> The intent is to get "all 1s", which is true if the integer
> representation is 2's complement; that's an implicit assumption
> of the code.

If that *were* an implicit assumption of the code, then the code
would be broken. But it's not. Pete's code AFAICT doesn't assume
anything earth-shattering about the integer representations used
by the target system.

> (This is the best reason not to use -1: the intent is not
> perfectly clear).

However, it *does* produce the right answer, which is a point in
its favor. ~0 might trap, and in any case I think (unsigned char)-1
has a bit more aesthetic value to it (YMMV, of course).

And if you want everything to be *perfectly* clear, then you might
want to consider a different programming language. C is just
full of 'for (i=0; a[i]; ++i)'s and 'while (*s++ = *t++)'s; if
a simple -1 throws you, then you're in trouble. :-)

-Arthur



Relevant Pages

  • Re: Layout Hell-o
    ... The C standard requires that integral types use a "pure binary ... the representation match the "native" pure binary representation used ... C implementations essentially always do use the native ... integers if the CPU uses big-endian integers. ...
    (comp.lang.cobol)
  • Re: Which pattern does this conform to?
    ... Andrew McDonagh wrote: ... concrete factory class; but I usually do allow the user to switch ... implementations behind the factory's interface. ... factory pattern offers the advantage that, if the user is careful, he ...
    (comp.object)
  • Re: is NULL-checking redundant in accessor-functions?
    ... >> implementations of free don't, ... pattern apparently represents two different numbers? ... The answer lies in the *interpretation* of the bits. ... and the bits represent a pointer? ...
    (comp.lang.c)
  • Re: what is trap representation?
    ... > of 'trap representation'. ... If the stored value of an object has such a representation ... Some implementations will issue some kind of exception, such as a SIGFPE, ... and therefore not part of standard C. ...
    (comp.lang.c.moderated)
  • Re: Google summer of code
    ... I wonder why implementations don't ... just wrap the C BSD sockets interface. ... The internal representation of an address wraps the C representation, ...
    (comp.lang.lisp)

Loading