Date: Mon, 13 Mar 2000 14:42:05 +1100 (EST) From: Bruce Evans <bde@zeta.org.au> To: John Polstra <jdp@polstra.com> Cc: keramida@ceid.upatras.gr, current@FreeBSD.ORG Subject: Re: MAX_UID ? Message-ID: <Pine.BSF.4.21.0003131422390.794-100000@alphplex.bde.org> In-Reply-To: <200003130151.RAA51453@vashon.polstra.com>
next in thread | previous in thread | raw e-mail | index | archive | help
On Sun, 12 Mar 2000, John Polstra wrote: > In article <20000313015009.A5653@hades.hell.gr>, > Giorgos Keramidas <keramida@ceid.upatras.gr> 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 majordomo@FreeBSD.org with "unsubscribe freebsd-current" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSF.4.21.0003131422390.794-100000>