Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a defined evaluation order for &= and |=?

If you have a C function which returns an integer, you could write a statement like this:

MyInt &= MyFunc();

...where we're using the bitwise-AND assignment operator.

The question is: is MyFunc() guaranteed to be executed, even if MyInt equals zero?

Likwise, if we used the bitwise-OR assignment operator (|=), would MyFunc() always be executed, even if MyInt were set to all ones?

Put another way: is lazy evaluation permitted in C for bitwise operators?

like image 330
Steve Melnikoff Avatar asked Aug 30 '09 15:08

Steve Melnikoff


1 Answers

No. Bitwise operators are not short-circuited. MyFunc() execution is guaranteed regardless of the value of MyInt.

like image 178
mmx Avatar answered Oct 17 '22 03:10

mmx