unsigned char x=1; x=(x++)<<3;
x=(++x)<<3; works it gives 16
Keil does have some problems with the pre- and post- increment/decrement operators when used within a more complicated expression. However, the value of the expression "x++" is x; the increment is not done until after the expression is evaluated - hence post increment! So
(x++)<<3
(x)<<3; x += 1
Since this equation produces undefined behavior, there is no right answer IMHO. See:
http://www.eskimo.com/~scs/C-faq/q3.3.html
x = x + 1 << 3;
To Keil Discussion Forum site: Add the following suggestion to your "Tips for Posting Measages". If you type an URL like
<pre>TheUrl</pre>
Actually, there is no need to use the <pre> and </pre> tags for URLs at all - the forum automatically makes URLs clickable (as it says in the Tips). It seems that it's this automatic conversion to a link which has gone wrong - it's caught the closing </pre> tag inside the closing </a> link tag! So yes, a space (or newline?) before the </pre> would probably have fixed it.
Sorry about that, I didn't mean to use the pre tag at all. - Mark
>Keil does have some problems with the pre- and >post- increment/decrement operators when used >within a more complicated expression Which problems your take in mind ? >(x++)<<3 >is equivalent to >(x)<<3; x += 1 >and it seems that MSVC has the "right" >answer. This is not exactly the same as original qiestion:
unsigned char x = 1; x=(x++) << 3;
unsigned char x = 1; unsigned char tmp; tmp = x; // get old value of 'x' x = x + 1; // x++ x = (tmp << 3); // Assign('=') have lowest priority
Please see http://www.eskimo.com/~scs/C-faq/q3.3.html
x = (x++) << 3;
Mark, thanks for good point: http://www.eskimo.com/~scs/C-faq/q3.3.html http://www.eskimo.com/~scs/C-faq/top.html It is a good place to understand why I intuitively avoid some ambiguous C-constructions :) On the other side, this is a 'style' quiestion. For example, in your case I always write as
x = (x + 1) << 3;
unsigned char x = 1; x = (x++) << 3;
LVALUE = EXPRESSION