Connecting Tech Pros Worldwide Help | Site Map

Binary constant macros

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 14th, 2005, 03:01 AM
Tom Torfs
Guest
 
Posts: n/a
Default Binary constant macros

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

  #2  
Old November 14th, 2005, 03:01 AM
CBFalconer
Guest
 
Posts: n/a
Default Re: Binary constant macros

Tom Torfs wrote:[color=blue]
>
> 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);
>[/color]
.... snip ...[color=blue]
>
> /* Sample usage:
> B8(01010101) = 85
> B16(10101010,01010101) = 43605
> B32(10000000,11111111,10101010,01010101) = 2164238933
> */[/color]

That is rather cute. You should wrap the macro file in an include
guard. The comma separation on byte boundaries especially adds
clarity. It does have some portability problems outside the
normal world of 8 bit bytes and 16/32 bit short/longs.

--
Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


  #3  
Old November 14th, 2005, 03:02 AM
Sergio Masci
Guest
 
Posts: n/a
Default Re: Binary constant macros

Tom Torfs <tomtorfs@village.uunet.be> wrote in message
news:b9d5cc04.0402260736.60017ce2@posting.google.c om...[color=blue]
> 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[/color]

<snip>

Very good!

Regards
Sergio Masci

http://www.xcprod.com/titan/XCSB - optimising structured PIC BASIC compiler


  #4  
Old November 14th, 2005, 03:04 AM
Pete Fenelon
Guest
 
Posts: n/a
Default Re: Binary constant macros

In comp.arch.embedded Tom Torfs <tomtorfs@village.uunet.be> wrote:[color=blue]
> 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:
>[/color]

Sweet and evil. I like it.

pete
--
pete@fenelon.com "there's no room for enigmas in built-up areas"
  #5  
Old November 14th, 2005, 03:04 AM
Leor Zolman
Guest
 
Posts: n/a
Default Re: Binary constant macros

On 26 Feb 2004 07:36:35 -0800, tomtorfs@village.uunet.be (Tom Torfs) wrote:
[color=blue]
>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[/color]

This may be closest thing I've seen to C++ template metaprogramming... in
C. And it doesn't even require the most up-to-date compilers! ;-)
Very useful.
-leor

Leor Zolman
BD Software
leor@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
  #6  
Old November 14th, 2005, 03:04 AM
Spehro Pefhany
Guest
 
Posts: n/a
Default Re: Binary constant macros

On Fri, 27 Feb 2004 20:59:22 +0000, the renowned Pete Fenelon
<pete@fenelon.com> wrote:
[color=blue]
>In comp.arch.embedded Tom Torfs <tomtorfs@village.uunet.be> wrote:[color=green]
>> 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:
>>[/color]
>
>Sweet and evil. I like it.[/color]

Me too. Suggest you write it up for Dr. Dobbs.

Best regards,
Spehro Pefhany
--
"it's the network..." "The Journey is the reward"
speff@interlog.com Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog Info for designers: http://www.speff.com
  #7  
Old November 14th, 2005, 03:07 AM
Uwe Hercksen
Guest
 
Posts: n/a
Default Re: Binary constant macros



Tom Torfs wrote:[color=blue]
>
> 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)
>[/color]
Hello,

thanks a lot, very helpful and very ingenious.
I think you will even manage to define macros for numbers with neither
decimal base nor a power of two as base.
But what is the use of compile time constant with a base of seven or
twelve? I can imagine no example, but maybe somebody else can. ;-)

Bye

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.