By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,640 Members | 2,095 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,640 IT Pros & Developers. It's quick & easy.

generating macros with another macro

P: n/a
hi all,

i want to define a group of consts for bits

#define BIT0 (1 << 0)
#define BIT1 (1 << 1)
....
#define BITi (1 << i)

is it a way to do it more elegant? or at least to put "i" as parameter
of other macro?

how i can do some like this???

#define BITX(i) #define BIT##i (1 << i)
tks in advance

Jun 18 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
rhXX wrote On 06/18/07 12:13,:
hi all,

i want to define a group of consts for bits

#define BIT0 (1 << 0)
#define BIT1 (1 << 1)
...
#define BITi (1 << i)

is it a way to do it more elegant? or at least to put "i" as parameter
of other macro?

how i can do some like this???

#define BITX(i) #define BIT##i (1 << i)
You cannot: A macro cannot generate a preprocessor
directive, even if the expansion resembles one.

Even if you could, what help would it be? Instead
of the group of #defines above, you'd have

#define BITX(i) ...something magical...
BITX(0)
BITX(1)
...
BITX(i)

That is, you'd need one *more* line than you already have.

Since the names of your macros are so descriptive ;-)
why not just use

#define BIT(i) (1 << (i)) /* maybe 1u? 1uL? 1uLL? */

.... and write BIT(0), BIT(2) instead of BIT0, BIT2?

--
Er*********@sun.com
Jun 18 '07 #2

P: n/a
On Jun 18, 9:13 pm, rhXX <rh00...@gmail.comwrote:
hi all,

i want to define a group of consts for bits

#define BIT0 (1 << 0)
#define BIT1 (1 << 1)
...
#define BITi (1 << i)

is it a way to do it more elegant? or at least to put "i" as parameter
of other macro?
You can write the code, for generating such sequence #define
BIT0..etc. and print it in the required .c file.
This is the only possible solution I envisage.
>
how i can do some like this???

#define BITX(i) #define BIT##i (1 << i)

tks in advance

Jun 18 '07 #3

P: n/a
tks eric and CryptiqueGuy

On Jun 18, 6:28 pm, Eric Sosman <Eric.Sos...@sun.comwrote:
You cannot: A macro cannot generate a preprocessor
directive, even if the expansion resembles one.
#define BITX(i) ...something magical...
BITX(0)
Since the names of your macros are so descriptive ;-)
why not just use
really i use:

#define BIT(bit) (1UL<<(bit))
#define BIT0 BIT(0)
....

i agree that this is only cosmetic .....

i wanted:
- avoid repeat "i" in #define BITi BIT(i)
- use BITi that "looks" as a constant and not BIT(i) that locks as a
function
On Jun 18, 6:37 pm, CryptiqueGuy <SRRajesh1...@gmail.comwrote:
You can write the code, for generating such sequence #define
BIT0..etc. and print it in the required .c file.
This is the only possible solution I envisage.
yes, but i was looking for a language or "academic" solution, not for
avoid writting, at the end, there are only 32 lines to write (for my
case)

tks again to all!

Jun 18 '07 #4

P: n/a
On Mon, 18 Jun 2007 16:13:41 +0000, rhXX wrote:
hi all,

i want to define a group of consts for bits

#define BIT0 (1 << 0)
#define BIT1 (1 << 1)
...
#define BITi (1 << i)

is it a way to do it more elegant? or at least to put "i" as parameter
of other macro?

how i can do some like this???

#define BITX(i) #define BIT##i (1 << i)
tks in advance
Do you need macros? I'd use:
enum
{ BIT0=0x0001
, BIT1=0x0002
/* etc */
};

Duncan

Jun 18 '07 #5

P: n/a

"rhXX" <rh*****@gmail.comwrote in message
news:11**********************@g4g2000hsf.googlegro ups.com...
hi all,

i want to define a group of consts for bits

#define BIT0 (1 << 0)
#define BIT1 (1 << 1)
...
#define BITi (1 << i)

is it a way to do it more elegant? or at least to put "i" as parameter
of other macro?
If you are new to C, never let yourself get caught up in "elegance."
Concentrate on correctness and readability. BITi is prone to
undefined behavior.

Often folks new to C, read really bad code that makes poor use
of macros. Then, they assume it is the way it should be done.

Whether this simple macro is an improvement on your code is
most likely subjective.
>
how i can do some like this???

#define BITX(i) #define BIT##i (1 << i)
tks in advance

Jun 19 '07 #6

P: n/a
rhXX wrote:
really i use:

#define BIT(bit) (1UL<<(bit))
#define BIT0 BIT(0)
...

i agree that this is only cosmetic .....

i wanted:
- avoid repeat "i" in #define BITi BIT(i)
- use BITi that "looks" as a constant and not BIT(i) that locks as a
function
Looks like a constant? Constant expressions work as well as constants
and have the advantage of showing you how the constant is generated.

--
Thad
Jun 19 '07 #7

P: n/a
tks to all for ideas and comments!

Jun 19 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.