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