home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!gatech!news.byu.edu!hamblin.math.byu.edu!sol.ctr.columbia.edu!zaphod.mps.ohio-state.edu!saimiri.primate.wisc.edu!ames!kronos.arc.nasa.gov!iscnvx!netcomsv!ulogic!hartman
- From: hartman@ulogic.UUCP (Richard M. Hartman)
- Newsgroups: comp.lang.c
- Subject: Re: single formula for number of bits set in a byte
- Message-ID: <566@ulogic.UUCP>
- Date: 6 Nov 92 19:33:36 GMT
- References: <1992Oct28.205725.31415@Cookie.secapl.com> <530@ulogic.UUCP> <6444@ucsbcsl.ucsb.edu>
- Organization: negligable
- Lines: 25
-
- In article <6444@ucsbcsl.ucsb.edu> 6500msd@ucsbuxa.ucsb.edu (Michael D'Errico) writes:
- >In article <530@ulogic.UUCP> hartman@ulogic.UUCP (Richard M. Hartman) writes:
- >
- >]without divides, and relying on the `==` operator returning 1 or 0:
- >
- >]#define isset(x,bit) ((x & bit) == bit)
- >] count = isset(x,0x80) + isset(x,0x40) + isset(x,0x20) + isset(x,0x10) +
- >] isset(x,0x08) + isset(x,0x04) + isset(x,0x02) + isset(x,0x01) ;
- >
- >It would be better to use
- >#define isset(x,bit) ((x & bit) ? 1:0)
-
- I have never met a compiler that yields other than 1 and 0 for
- equality and relational expressions.
-
- For C++ compilers this is guaranteed in the ARM (p74, section 5.9).
- Unfortunately I don't have the ANSI C compiler document available
- but I would suspect this is guarenteed there also.
-
- -Richard Hartman
- hartman@ulogic.COM
-
- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
- Moderation in all things.... ....especially moderation.
-
-