short ciruit or buggy?

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
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.
| is not EXCLUSIVE OR , it is just plain OR or INCLUSIVE OR
Topic archived. No new replies allowed.