# Bit operator in expression language

**URL:** <https://forum.inductiveautomation.com/t/bit-operator-in-expression-language/84178>\
**Category:** Ignition\
**Created:** [January 12, 2024, 10:58am UTC](https://forum.inductiveautomation.com/t/bit-operator-in-expression-language/84178 "2024-01-12T10:58:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomas\_Spachinger](https://avatars.discourse-cdn.com/v4/letter/t/d6d6ee/32.png) [@Thomas\_Spachinger](https://forum.inductiveautomation.com/u/Thomas_Spachinger)\
**Post date:** [January 12, 2024, 10:58am UTC](https://forum.inductiveautomation.com/t/bit-operator-in-expression-language/84178/1 "2024-01-12T10:58:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [January 12, 2024, 11:13am UTC](https://forum.inductiveautomation.com/t/bit-operator-in-expression-language/84178/2 "2024-01-12T11:13:29Z")

</div>

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](https://docs.inductiveautomation.com/display/DOC81/Expression+Language+and+Syntax#ExpressionLanguageandSyntax-Operators)

See also, [Wiki - how to post code on this forum](https://forum.inductiveautomation.com/t/wiki-how-to-post-code-on-this-forum/77831)

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [January 12, 2024, 1:26pm UTC](https://forum.inductiveautomation.com/t/bit-operator-in-expression-language/84178/3 "2024-01-12T13:26:57Z")

</div>

> [@Transistor](#):
>
> You're using Bitwise AND where you should be using Logical AND.

Pretty sure the OP wants the bitwise behavior.

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

---

<div class="post-metadata">

**Author:** ![Thomas\_Spachinger](https://avatars.discourse-cdn.com/v4/letter/t/d6d6ee/32.png) [@Thomas\_Spachinger](https://forum.inductiveautomation.com/u/Thomas_Spachinger)\
**Post date:** [January 15, 2024, 11:45am UTC](https://forum.inductiveautomation.com/t/bit-operator-in-expression-language/84178/4 "2024-01-15T11:45:14Z")

</div>

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 &.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [January 15, 2024, 12:59pm UTC](https://forum.inductiveautomation.com/t/bit-operator-in-expression-language/84178/5 "2024-01-15T12:59:41Z")

</div>

> [@Thomas\_Spachinger](#):
>
> 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/](https://introcs.cs.princeton.edu/java/11precedence/)
