Morning.
Both operators can be used on any integer values, regardless of the bit size of that value. The reason why OR works as expected in the conditional is C (and Flowcode) treat 'true' as 'not false', therefore any non-zero value is true. For conditionals it is not usually as efficient, however, and it is usually better to use '||'.
There are several differences between OR ('|') and logical-or ('||'), here are some:
Logical-or will evaluate the result to 'true' or 'false', bitwise evaluates to the bitwise value:
(Though just to confuse matters further I have seen optimised compilers that evaluate '(5 || 6) == 5' when the result isnt stored in a variable!)
Logical-or will not necessarily execute the entire expression, and will exit the condition when the result is guaranteed to be true, so:
Will not execute complex_expression as there is no point. Bitwise-or will execute this, as it will effect the result. This makes it execute faster in most situations.
Logical-or has a guaranteed execution order (left to right), where as bitwise-OR has no guarantee and is compiler/platform dependent, so:
Code: Select all
is_function_valid || call_function()
Will only execute call_function() if it is valid, where as:
Code: Select all
is_function_valid | call_function()
Will not only execute the function regardless, but may even call it before is_function_valid is even processed. I believe the BoostC compiler works this way (it does with addition, at any rate).
The operator precedence is slightly different, but this isnt a concern for most operations, and brackets shouldnt be required for simple comparison expressions (though they should be provided for clarity).
I hope any of this is of use,
Jonny