473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
Nov 14 '05 #1
6 19801
Tom Torfs wrote:

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


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 (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #2
Tom Torfs <to******@village.uunet.be> wrote in message
news:b9**************************@posting.google.c om...
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


<snip>

Very good!

Regards
Sergio Masci

http://www.xcprod.com/titan/XCSB - optimising structured PIC BASIC compiler
Nov 14 '05 #3
In comp.arch.embedded Tom Torfs <to******@village.uunet.be> wrote:
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:


Sweet and evil. I like it.

pete
--
pe**@fenelon.com "there's no room for enigmas in built-up areas"
Nov 14 '05 #4
On 26 Feb 2004 07:36:35 -0800, to******@village.uunet.be (Tom Torfs) wrote:
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


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
le**@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
Nov 14 '05 #5
On Fri, 27 Feb 2004 20:59:22 +0000, the renowned Pete Fenelon
<pe**@fenelon.com> wrote:
In comp.arch.embedded Tom Torfs <to******@village.uunet.be> wrote:
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:


Sweet and evil. I like it.


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

Best regards,
Spehro Pefhany
--
"it's the network..." "The Journey is the reward"
sp***@interlog.com Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog Info for designers: http://www.speff.com
Nov 14 '05 #6


Tom Torfs wrote:

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)

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

Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Michael Winter | last post by:
In a recent post ("About C error" by Victor, 21 Sep 2003), comments were made about the poster's use of macros. What I would like to know is why they are considered bad? I'm not referring to...
37
by: hasadh | last post by:
Hello, probably this may be a simple qn to u all but I need an answer plz. In my software i used macros like OK,TRUE,FALSE,FAILURE . A friend who included this code as a library into his module...
6
by: Kelvin | last post by:
Hi everyone: when we wanna use hex numbers in C, we usually write something like: int hex_num = 0x12F9; but how can I declare a binary number in a similar way by putting some leading words to...
3
by: Stephen Mayes | last post by:
Is there any way to represent a literal numerical value in binary? My wish would be something like 0b0011 instead of the hex 0x0F. ( if this is even the same. ) Sometimes I find it's easier to...
15
by: Scott | last post by:
Hi All, I have the following C code in a header file, outside of any functions: const float X = 50; const float Y = 100 * X; But, when compiling, I get an error: initializer element is...
5
by: serrand | last post by:
Hello all, is there a way in order to write binary numbers as hexa or octal in c ? Xavier
33
by: Robert Seacord | last post by:
When writing C99 code is a reasonable recommendation to use inline functions instead of macros? What sort of things is it still reasonable to do using macros? For example, is it reasonable to...
6
by: Shawn B. | last post by:
Greetings, I've been up and down google on this one and clearly I don't know what search terms to use because I can't locate how to do this for binary values (or hex)... in C#, we can specify...
9
by: tguclu | last post by:
Hi I'm trying to make a CRC calculator and for test purposes i'm writing some test routines. I have the pre-calculated crc values for some binary numbers and i want to use them in my code. Is it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.