On Sun, 12 Mar 2000, John Polstra wrote:
> In article <[EMAIL PROTECTED]>,
> Giorgos Keramidas <[EMAIL PROTECTED]> wrote:
> > On Sun, Mar 12, 2000 at 05:59:09AM +0000, Paul Richards wrote:
> > >
> > > Are expressions like ((uid_t)0-1) portable/safe ? Maybe that's a better
> > > way of approaching this.
> >
> > To get the all-1's number, maybe it's better to use ((uid_t)~0), but
> > that is a rather controversial topic anyway.
>
> That works, but on machines like the Alpha where longs are bigger
> than ints it only works by virtue of sign extension. Our existing
> headers seem to prefer ((uid_t)0-1). That's what is used in the
> i386's <machine/limits.h>.
All 3 of these are broken in general. ((uquad_t)0-1) in <machine/limits.h>
works because it is known that uquad_t is no smaller than int. If foo_t is
smaller than int, then (foo_t)0 in an expression gets promoted to plain 0,
which is rarely what you want.
((uid_t)~(uid_t)0) is probably best. The first cast gives a sufficient
number of 0 bits and the second cast discards unwanted 1 bits.
-1 is sometimes used instead of ~0 because there is a good rule for
converting -1 to an unsigned type. I can never quite remember this
rule, so I prefer to use ~something. Part of the rule is that
(unsigned int)-1 has all bits 1 even if -1 doesn't have all bits 1.
Bruce
To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-current" in the body of the message