Bit operator in expression language

Hi together,

i just wanted to ask, if it is a bug, that within the expression language, the bit operator & is handled after the math operators or if it is supposed to act so:

i = 47 (for example bound to a tag)
if(i&64>0, true, false)
this will result in true, cause it will do 64>0 (true/1) before the & - so every odd number becomes true

if you want the "correct" solution it has to look like:
if((i&64)>0, true, false)
or just
i&64 or toBoolean(i&64)

took me quite a while and help from an external to figure out, whats going on

You're using Bitwise AND where you should be using Logical AND.

if(47 & 64, true, false) returns false.
if(48 & 64, true, false) returns false.
if(47 && 64, true, false) returns true.
if(47 && 64, true, false) returns true.
if(0 && 64, true, false) returns false.

So, you don't need the > 0 and since you are returning a bool you don't need the if(). You can replace the whole lot with the expression,
i && 64

https://docs.inductiveautomation.com/display/DOC81/Expression+Language+and+Syntax#ExpressionLanguageandSyntax-Operators

See also, Wiki - how to post code on this forum

Pretty sure the OP wants the bitwise behavior.

In which case the entire expression can be i & 64. No if(), and no >0.

3 Likes

Yes exactly - wanted the bitwise operation and also had the solution as written in the post. I just was wondering, why the mathematical operation > is executed before the &.

I don't know this for a fact, as the Expression language is IA's thing, but I highly suspect that it has to do with the order of precedence in Java, in which the > operator has a higher precedence than the & operator.

https://introcs.cs.princeton.edu/java/11precedence/

3 Likes