home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / c / 16036 < prev    next >
Encoding:
Text File  |  1992-11-06  |  1.8 KB  |  49 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!unislc!ttobler
  3. From: ttobler@unislc.uucp (Trent Tobler)
  4. Subject: Re: Binary Representation
  5. X-Newsreader: Tin 1.1 PL5
  6. References: <1992Nov5.160215.25718@nosc.mil>
  7. Message-ID: <1992Nov5.222742.7875@unislc.uucp>
  8. Organization: Unisys Corporation SLC
  9. Date: Thu, 5 Nov 1992 22:27:42 GMT
  10. Lines: 37
  11.  
  12. Ray Mitchell (mitch@nosc.mil) wrote:
  13. : It seems that since C has bitwise operators and its treatment of pointers
  14. : is so close to the underlying hardware, there should be a way of representing
  15. : constants in binary in addition to decimal, hex, and octal.  Does anyone
  16. : know why this was not done?  Maybe I'm the only one who sees any value in
  17. : it.  (Please don't tell me to use assembly if I want binary).  I've
  18. : contemplated some macro way of implementing a binary representation but
  19. : have not come up with anything reasonable.  Any ideas?
  20.  
  21. I can think of three ways:
  22.  
  23. #define BIN4(a,b,c,d)    ((a<<3)|(b<<2)|(c<<1)|(d))
  24. #define BIN8(a,b,c,d,e,f,g,h) ((BIN4(a,b,c,d)<<4)|(BIN4(e,f,g,h)))
  25. #define BIN16(a,b,c,d,e,f,g,h) ((BIN4(a,b,c,d)<<4)|(BIN4(e,f,g,h)))
  26.  /* you get the idea */
  27.  
  28. #define BINARY(dec)  ((dec%10)|((dec%100)/10)|((dec%1000)/100)\
  29.         ((dec%10000)/1000)|((dec%100000)/10000)|((dec%1000000)/100000)\
  30.         /* you get the idea */ )
  31.     /* has the problem of only being able to do 10 digits */
  32.     /* also could make a hybrid between this one and the first one and */
  33.     /* get more */
  34.  
  35.  
  36. /* ANSI C */
  37. #define BINARY(binary)    (strtol( #binary, (char **) 0, 2))
  38. /* K&R C */
  39. #define BINARY(binary)  (strtol( "binary", (char **) 0, 2))
  40.  
  41.  
  42. Probably the reason for not including binary in the C language is that
  43. binary tends to be long and bulky... and it is trivial to convert binary
  44. to hex or octal in ones head.
  45.  
  46. --
  47.   Trent Tobler - ttobler@unislc.slc.unisys.com
  48.