468,484 Members | 2,155 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,484 developers. It's quick & easy.

Operation undefined

Hi,

for this code

int main( void )
{
unsigned short a= 10;
a = (a <<= 1) ^ 4129; //warning concerns this line
return 0;
}

gcc issues the warning:

warning: operation on `a' may be undefined
The problem is "(a <<=1 )" before the XOR operator, but
why might this expression lead to undefined behavior?

Regards,
Chris
Jul 31 '06 #1
3 3331
Christian Christmann wrote:
Hi,

for this code

int main( void )
{
unsigned short a= 10;
a = (a <<= 1) ^ 4129; //warning concerns this line
return 0;
}

gcc issues the warning:

warning: operation on `a' may be undefined
The problem is "(a <<=1 )" before the XOR operator, but
why might this expression lead to undefined behavior?
The operation is undefined because a is modified twice without an
intervening sequence point. Why not just use "a = (a << 1) ^ 4129" ?

Robert Gamble

Jul 31 '06 #2
Christian Christmann wrote:
Hi,

for this code

int main( void )
{
unsigned short a= 10;
a = (a <<= 1) ^ 4129; //warning concerns this line
return 0;
}
#include <stdio.h>

int main(void)
{
unsigned short a = 10;
#if 0
/* the following is a variation on 'i = i++;' covered by the FAQ */
a = (a <<= 1) ^ 4129;
#endif
/* but this should work */
a = (a << 1) ^ 4129;
printf("a was 10, and is now %u\n", a);
/* and this should work */
a = 10;
a = (a << 1);
a ^= 4129;
printf("a was 10, and is now %u\n", a);
return 0;
}

Jul 31 '06 #3
On 2006-07-31, Christian Christmann <pl*****@yahoo.dewrote:
Hi,

for this code

int main( void )
{
unsigned short a= 10;
a = (a <<= 1) ^ 4129; //warning concerns this line
return 0;
}

gcc issues the warning:

warning: operation on `a' may be undefined
The problem is "(a <<=1 )" before the XOR operator, but
why might this expression lead to undefined behavior?
I'm pretty sure this is a (potential?) sequence-point error, and
that is what gcc is warning you about. It appears to me that using
(a <<= 1) is logically equivilant to (a << 1) in this context:

int main (void)
{
unsigned short a = 10;
a = (a << 1) ^ 4129;
return 0;
}

--
Andrew Poelstra <website down>
To reach my email, use <email also down>
New server ETA: 42
Jul 31 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

43 posts views Thread by Mehta Shailendrakumar | last post: by
30 posts views Thread by jimjim | last post: by
reply views Thread by relaxedrob | last post: by
24 posts views Thread by Nishu | last post: by
11 posts views Thread by sethukr | last post: by
1 post views Thread by subramanian100in | last post: by
16 posts views Thread by foolsmart2005 | last post: by
reply views Thread by NPC403 | last post: by
2 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.