Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
02/27/04 01:25
Read: times


 
#65591 - Binary constant macros
From time to time, someone asks about using binary constants in C. The following post appeared in the comp.lang.c and comp.arch.embedded newsgroups on the subject of binary constants. Since, as far as I can recall, there has been only one other Forum contributor that has said that they read Usenet's comp.arch.embedded (other than me), I am posting the Usenet article verbatim here for those who care to have some kind of binary constant facility in C.
================================================================

From: tomtorfs@village.uunet.be (Tom Torfs)
Newsgroups: comp.lang.c,comp.arch.embedded
Subject: Binary constant macros
Date: 26 Feb 2004 07:36:35 -0800
Organization: http://groups.google.com
Lines: 66
Message-ID: <b9d5cc04.0402260736.60017ce2@posting.google.com>
NNTP-Posting-Host: 146.103.254.11
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
NNTP-Posting-Date: Thu, 26 Feb 2004 15:36:35 +0000 (UTC)

Hello All,

I've been missing the lack of support for binary numeric literals in
C. To get around it I wrote the following handy macros, which allows
you to simply write something like:

whatever = B8(10101010);

and will translate as:

whatever = 85;

(compile-time constant)

Code below... hopefully it's useful to some of you as well.

greetings,
Tom

/* Binary constant generator macro
   By Tom Torfs - donated to the public domain
*/

/* All macro's evaluate to compile-time constants */

/* *** helper macros *** /

/* turn a numeric literal into a hex constant
   (avoids problems with leading zeroes)
   8-bit constants max value 0x11111111, always fits in unsigned long
*/
#define HEX__(n) 0x##n##LU

/* 8-bit conversion function */
#define B8__(x) ((x&0x0000000FLU)?1:0)	
               +((x&0x000000F0LU)?2:0)	
               +((x&0x00000F00LU)?4:0)	
               +((x&0x0000F000LU)?8:0)	
               +((x&0x000F0000LU)?16:0)	
               +((x&0x00F00000LU)?32:0)	
               +((x&0x0F000000LU)?64:0)	
               +((x&0xF0000000LU)?128:0)

/* *** user macros *** /

/* for upto 8-bit binary constants */
#define B8(d) ((unsigned char)B8__(HEX__(d)))

/* for upto 16-bit binary constants, MSB first */
#define B16(dmsb,dlsb) (((unsigned short)B8(dmsb)<<8)	
			+ B8(dlsb))

/* for upto 32-bit binary constants, MSB first */
#define B32(dmsb,db2,db3,dlsb) (((unsigned long)B8(dmsb)<<24)	 
				  + ((unsigned long)B8(db2)<<16) 
				  + ((unsigned long)B8(db3)<<8)	 
				  + B8(dlsb))

/* Sample usage:
      B8(01010101) = 85
      B16(10101010,01010101) = 43605
      B32(10000000,11111111,10101010,01010101) = 2164238933
*/

greetings,
Tom



List of 9 messages in thread
TopicAuthorDate
Binary constant macros            01/01/70 00:00      
   RE: Binary constant macros            01/01/70 00:00      
      RE: Binary constant macros            01/01/70 00:00      
   RE: Binary constant macros            01/01/70 00:00      
      RE: Binary constant macros            01/01/70 00:00      
         RE: Binary constant macros            01/01/70 00:00      
            RE: Binary constant macros            01/01/70 00:00      
               RE: Binary constant macros            01/01/70 00:00      
   RE: Binary constant macros            01/01/70 00:00      

Back to Subject List