Any idea about this warning

S

Sunil

Hi all,

While resolving the below sentence

#ifdef (CONFIG_COMMANDS & CFG_CMD_IDE)

The compiler is giving warning as

warning: the right operand of "&" changes sign when promoted

Is it related to stack.

Regards,
Sunil.
 
S

Sunil

Sorry for the mistake,
It is #if

But the remaining question is correct.

Question:
-------------
While resolving the below sentence

#define CONFIG_COMMANDS (CFG_CMD_ALL & ~CFG_CMD_NONSTD)
#if (CONFIG_COMMANDS & CFG_CMD_IDE)


The compiler is giving warning for the second line

warning: the right operand of "&" changes sign when promoted

Is it related to stack.
 
W

William J. Leary Jr.

Sunil said:
Sorry for the mistake,
It is #if

But the remaining question is correct.

Question:
-------------
While resolving the below sentence

#define CONFIG_COMMANDS (CFG_CMD_ALL & ~CFG_CMD_NONSTD)
#if (CONFIG_COMMANDS & CFG_CMD_IDE)


The compiler is giving warning for the second line

warning: the right operand of "&" changes sign when promoted

OK. What are CFG_CMD_ALL and CFG_CMD_NONSTD ?

Perhaps you needed ! (logical not) rather than ~ (mathematical complement)
before CFG_CMD_NONSTD ?
Is it related to stack.

Oh, that's a question. I misread that as "It is related to the stack." rather
than "Is it related to the stack?".

Probably not.

- Bill
 
M

Mac

Hi all,

While resolving the below sentence

#ifdef (CONFIG_COMMANDS & CFG_CMD_IDE)

The compiler is giving warning as

warning: the right operand of "&" changes sign when promoted

Is it related to stack.

Regards,
Sunil.

The warning seems perfectly clear.

What exactly do you want to know?

--Mac
 
M

Marc Boyer

Le 28-10-2005 said:
Hi all,

While resolving the below sentence

#ifdef (CONFIG_COMMANDS & CFG_CMD_IDE)

The compiler is giving warning as

warning: the right operand of "&" changes sign when promoted

Without any other information, I assume that one operand
is a signed integer type, the other an unsigned one,
and the compiler warns you that one of both will be
promoted and change its sign.

Look in your code to know wich one is signed, which
one is not, and in a C book the promotion rules.

Marc Boyer
 
W

William J. Leary Jr.

I see I could have explained that a bit better earlier this morning.

William J. Leary Jr. said:
OK. What are CFG_CMD_ALL and CFG_CMD_NONSTD ?

Perhaps you needed ! (logical not) rather than ~ (mathematical complement)
before CFG_CMD_NONSTD ?

After expansion, the second line becomes:

#if ((CFG_CMD_ALL & ~CFG_CMD_NONSTD) & CFG_CMD_IDE)

It's complaining about the first &, not the second.

Again, try ! instead.

But also, how is CFG_CMD_NONSTD defined? It may make a difference, especially
if it's

#define CFG_CMD_NONSTD

for true and just not defined at all if not. In this case, if it's not
defined, your statement expands to:

#if ((CFG_CMD_ALL & ~) & CFG_CMD_IDE)

which has going to be a problem.

- Bill
 
M

Michael Wojcik

Presumably CFG_CMD_ALL represents a bit vector, and CFG_CMD_NONSTD
one or more of the bits. The OP should have provided this
information (and the value of CFG_CMD_IDE), though.
After expansion, the second line becomes:

#if ((CFG_CMD_ALL & ~CFG_CMD_NONSTD) & CFG_CMD_IDE)

It's complaining about the first &, not the second.

Again, try ! instead.

I strongly suspect that is wrong. The use of bitwise-and (&) rather
than logical-and (&&), and the general form of the expression,
strongly suggest that this expression is meant to test if a
particular bit (or set of bits) is set in the left operand.
But also, how is CFG_CMD_NONSTD defined?

This is indeed the pertinent question. I suspect it's something
like

#define CFG_CMD_NONSTD 2

or

#define CFG_CMD_NONSTD (1<<1)

or perhaps something like

#define CFG_CMD_A 0x01
#define CFG_CMD_B 0x02

/* vector of "nonstandard" commands */
#define CFG_CMD_NONSTD (CFG_CMD_A | CFG_CMD_B)

And the diagnostic that the OP is getting is probably due to using
the binary-negation operator (~) on a signed integer constant, which
will produce a result with the opposite sign. (In two's-complement,
for example, ~1 == -2.)

The simple fix would seem to be changing the definition of
CFG_CMD_NONSTD, or of the other macros that compose it if it's an
expression, to use unsigned integer constants, as in:

#define CFG_CMD_NONSTD 2u

--
Michael Wojcik (e-mail address removed)

Q: What is the derivation and meaning of the name Erwin?
A: It is English from the Anglo-Saxon and means Tariff Act of 1909.
-- Columbus (Ohio) Citizen
 
W

William J. Leary Jr.

Michael Wojcik said:
I strongly suspect that is wrong.

You're right. I should have tried compiling it here.
This is indeed the pertinent question. I suspect it's something
like

#define CFG_CMD_NONSTD 2

Actually, that works (here).

I've used this as test code:

#define CFG_CMD_ALL 1
#define CFG_CMD_NONSTD 2
#define CFG_CMD_IDE 1
#define CONFIG_COMMANDS (CFG_CMD_ALL & ~CFG_CMD_NONSTD)
#if CFG_CMD_ALL
#if CONFIG_COMMANDS
#if (CONFIG_COMMANDS & CFG_CMD_IDE)

This all compiles, until it complains that I didn't close the #ifs.

When I change any of those to remove the value, as in
#define CFG_CMD_ALL
I get
x.c(5) : fatal error C1017: invalid integer constant expression

So, what they're defined as is entirely relevant.

Oddly, this code looks familiar to me. I recall working an open source program
which ran on DOS, Unix, WIndows and Linux. It had something like this:

#ifdef PLATFORM_Z
#define CFG_CMD_ALL
/* #define CFG_CMD_NONSTD - Platform Z is standard*/
#define CFG_CMD_IDE
#endif

#ifdef PLATFORM_Y
#define CFG_CMD_ALL
#define CFG_CMD_NONSTD
/* #define CFG_CMD_IDE - Platform Y doesn't support the IDE */
#endif

then used #ifdefs and #ifndefs with them.

#ifdef CFG_CMD_ALL
#ifndef CFG_CMD_NONSTD
#define CONFIG_COMMANDS
#else
#undefine CONFIG_COMMANDS
#endif
... and so on...

- Bill
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,016
Latest member
TatianaCha

Latest Threads

Top