short ciruit or buggy?

Apr 28, 2010 at 3:43pm
This snippet ran as expected. It displayed 3 ( 1 exclusive or 2).
However, when using
unsigned short properties = (buf[idx++] | buf[idx++]);
it displayed incorrect value 1. did compiler do a short circuit evaluation?


int main() {
int idx = 0;
unsigned char buf[] = {0x01,0x02};
unsigned char low = buf[idx++];
unsigned char high = buf[idx++];
unsigned short properties = low | high; //(buf[idx++] | buf[idx++]);

std::cout<<"props:"<<std::hex<<properties<<std::endl;

return 0;
}
Last edited on Apr 28, 2010 at 5:32pm
Apr 28, 2010 at 6:40pm
It didn't display an incorrect value... the behavior of your code is undefined
by the standard.

Specifically, the standard does not mandate when either of the idx post-increments
are performed with respect to that line of code.

For example, it could read buf[idx], then increment idx, then read buf[idx], then
increment idx. Or it could read buf[idx] twice, then increment idx twice.
Apr 28, 2010 at 7:42pm
| is not EXCLUSIVE OR , it is just plain OR or INCLUSIVE OR
Topic archived. No new replies allowed.