473,385 Members | 1,359 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.

generating macros with another macro

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

"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
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
tks to all for ideas and comments!

Jun 19 '07 #8

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

Similar topics

21
by: Chris Reedy | last post by:
For everyone - Apologies for the length of this message. If you don't want to look at the long example, you can skip to the end of the message. And for the Python gurus among you, if you can...
16
by: mike420 | last post by:
Tayss wrote: > > app = wxPySimpleApp() > frame = MainWindow(None, -1, "A window") > frame.Show(True) > app.MainLoop() > Why do you need a macro for that? Why don't you just write
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: seberino | last post by:
I've been reading the beloved Paul Graham's "Hackers and Painters". He claims he developed a web app at light speed using Lisp and lots of macros. It got me curious if Lisp is inherently faster...
12
by: David Powell | last post by:
Because my work area won't have an Access programmer next year, I've been asked to rebuild their coded application as a set of modular tools. The idea is that a non-programmer will be able to...
47
by: Emil | last post by:
Is there any hope that new versions of PHP will support macros similar to C or C++? I've searched manual and didn't find anything except define directive, but it can be used to define constant...
1
by: atbusbook | last post by:
macros are sort of like c macros but more powerful. they are the manafestation of treating code like data. lisp code is just a textual representation of a data structure that is the list. this...
7
by: aaragon | last post by:
Hi everyone, I have a simple question. I'm trying to make a macro in one file so I can use it in main.cpp. The idea is that I the user of my code has simple to type the macro definition to replace...
6
by: Thant Tessman | last post by:
Back before C++ templates, I was taught a trick whereby one built C macros that built other C macros. I can't remember how the heck we did it. The problem is that there seems to be no standard way...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.