1. This forum section is a read-only archive which contains old newsgroup posts. If you wish to post a query, please do so in one of our main forum sections (here). This way you will get a faster, better response from the members on Motherboard Point.

Binary constant macros

Discussion in 'Embedded' started by Tom Torfs, Feb 26, 2004.

  1. Tom Torfs

    Tom Torfs Guest

    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.


    /* 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) \

    /* *** 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

    Tom Torfs, Feb 26, 2004
    1. Advertisements

  2. Tom Torfs

    CBFalconer Guest

    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.
    CBFalconer, Feb 26, 2004
    1. Advertisements

  3. Tom Torfs

    Sergio Masci Guest


    Very good!

    Sergio Masci

    http://www.xcprod.com/titan/XCSB - optimising structured PIC BASIC compiler
    Sergio Masci, Feb 26, 2004
  4. Tom Torfs

    Oliver Betz Guest

    (Tom Torfs) wrote:

    [whatever = B8(10101010); using long hex constants}

    really clever, thanks!

    Oliver Betz, Feb 27, 2004
  5. Tom Torfs

    Pete Fenelon Guest

    Sweet and evil. I like it.

    Pete Fenelon, Feb 27, 2004
  6. Tom Torfs

    Leor Zolman Guest

    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 Zolman
    BD Software

    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
    Leor Zolman, Feb 27, 2004
  7. Me too. Suggest you write it up for Dr. Dobbs.

    Best regards,
    Spehro Pefhany
    Spehro Pefhany, Feb 27, 2004
  8. Between this and the URL of the unmaintainable code site it has been a
    glorious day for comp.arch.embedded! Thanks, Tom. Your contribution has
    already enhanced my current project!
    Michael R. Kesti, Feb 28, 2004
  9. Tom Torfs

    Uwe Hercksen Guest


    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. ;-)

    Uwe Hercksen, Mar 1, 2004
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.