Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > generating macros with another macro

Reply
Thread Tools

generating macros with another macro

 
 
rhXX
Guest
Posts: n/a
 
      06-18-2007
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

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      06-18-2007
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?

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
CryptiqueGuy
Guest
Posts: n/a
 
      06-18-2007
On Jun 18, 9:13 pm, rhXX <(E-Mail Removed)> 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?


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



 
Reply With Quote
 
rhXX
Guest
Posts: n/a
 
      06-18-2007
tks eric and CryptiqueGuy

On Jun 18, 6:28 pm, Eric Sosman <(E-Mail Removed)> wrote:

> 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 <(E-Mail Removed)> wrote:

> 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!

 
Reply With Quote
 
Duncan Muirhead
Guest
Posts: n/a
 
      06-18-2007
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



 
Reply With Quote
 
Barry
Guest
Posts: n/a
 
      06-19-2007

"rhXX" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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
>



 
Reply With Quote
 
rhXX
Guest
Posts: n/a
 
      06-19-2007
tks to all for ideas and comments!

 
Reply With Quote
 
Thad Smith
Guest
Posts: n/a
 
      06-19-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
macros-loop? calling macros X times? Andrew Arro C Programming 2 07-24-2004 09:52 AM
Explanation of macros; Haskell macros mike420@ziplip.com Python 80 11-07-2003 02:22 AM
Re: Explanation of macros; Haskell macros Michael T. Babcock Python 0 11-03-2003 01:54 PM
Re: Explanation of macros; Haskell macros mike420@ziplip.com Python 5 11-01-2003 01:09 AM
Re: Explanation of macros; Haskell macros mike420@ziplip.com Python 1 10-07-2003 04:07 PM



Advertisments