Jump to content
Sign in to follow this  
neilbuck

Macro Expansion / && Operator

Recommended Posts

Bug description:

I think this is a bug, it's certainly unexpected behaviour.

 

I use the following macro to test whether a bit is set:

#define test_bit(x,y) 	 ((x&(1<<y))!=0)

Example usage:

 

When to read from usart:

 

if (test_bit(pie1, RCIE) && test_bit(pir1,RCIF))
         {

          }

 

and it works as expected, however I would also expect the following definition of test_bit to work as well (and it's fewer operations):

#define test_bit(x,y) 	 (x&(1<<y))

 

however the result of the if statment is different to the first implementation. i.e. one evaluates to true when the other evaluates to false. I'm not sure whether it's the macro expansion which is wrong or whether it's to do with the && operator and operator precedence.

 

Expected behaviour:

I would expect (x&(1<<y)) to have the same logical evaluation as ((x&(1<<y))!=0) in the above expression.

 

Is the problem 100% reproduceable:

everytime

 

IDE version: 5.8

Compiler: BoostC

Compiler version: 1.9.3

Target device: PIC16f876a

OS: XP Pro SP1

Share this post


Link to post
Share on other sites

neilbuck,

 

looks like a bug, even code with no macros fails:

 

void main()
{
char res = 0;
char myByte;

myByte = 1;
if ( (myByte & 1) &&  (myByte & 2) )	
{
 res |= 4;
}

// res should be 0 here
while( 1 );
}

 

Regards

Dave

Share this post


Link to post
Share on other sites

This happens to be an over-optimization bug. If you turn compiler optimization off (by adding -O0 option into the compiler 'extra' field) this bug is gone. Will fix it by the next release.

 

Regards,

Pavel

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...