home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / c / 16108 < prev    next >
Encoding:
Internet Message Format  |  1992-11-08  |  1.4 KB

  1. 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
  2. From: hartman@ulogic.UUCP (Richard M. Hartman)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: single formula for number of bits set in a byte
  5. Message-ID: <566@ulogic.UUCP>
  6. Date: 6 Nov 92 19:33:36 GMT
  7. References: <1992Oct28.205725.31415@Cookie.secapl.com> <530@ulogic.UUCP> <6444@ucsbcsl.ucsb.edu>
  8. Organization: negligable
  9. Lines: 25
  10.  
  11. In article <6444@ucsbcsl.ucsb.edu> 6500msd@ucsbuxa.ucsb.edu (Michael D'Errico) writes:
  12. >In article <530@ulogic.UUCP> hartman@ulogic.UUCP (Richard M. Hartman) writes:
  13. >
  14. >]without divides, and relying on the `==` operator returning 1 or 0:
  15. >
  16. >]#define isset(x,bit)    ((x & bit) == bit)
  17. >]    count = isset(x,0x80) + isset(x,0x40) + isset(x,0x20) + isset(x,0x10) +
  18. >]            isset(x,0x08) + isset(x,0x04) + isset(x,0x02) + isset(x,0x01) ;
  19. >
  20. >It would be better to use
  21. >#define isset(x,bit) ((x & bit) ? 1:0)
  22.  
  23. I have never met a compiler that yields other than 1 and 0 for
  24. equality and relational expressions.
  25.  
  26. For C++ compilers this is guaranteed in the ARM (p74, section 5.9).
  27. Unfortunately I don't have the ANSI C compiler document available
  28. but I would suspect this is guarenteed there also.
  29.  
  30.         -Richard Hartman
  31.         hartman@ulogic.COM
  32.  
  33. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  34. Moderation in all things....                 ....especially moderation.
  35.  
  36.