# BinEnc Expression Function Not Working as Documented

**URL:** <https://forum.inductiveautomation.com/t/binenc-expression-function-not-working-as-documented/92090>\
**Category:** Ignition\
**Tags:** ignition81\
**Created:** [June 19, 2024, 1:48pm UTC](https://forum.inductiveautomation.com/t/binenc-expression-function-not-working-as-documented/92090 "2024-06-19T13:48:04Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Starlyn\_Olivero](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@Starlyn\_Olivero](https://forum.inductiveautomation.com/u/Starlyn_Olivero)\
**Post date:** [June 19, 2024, 1:48pm UTC](https://forum.inductiveautomation.com/t/binenc-expression-function-not-working-as-documented/92090/1 "2024-06-19T13:48:04Z")

</div>

Hi,

I wanted to convert an array of booleans to Integer or Long using the **[binEnc](https://www.docs.inductiveautomation.com/docs/8.1/appendix/expression-functions/logic/binEnc)** functions to handle alarms in one value using the bit state.

**binEnc** fails to handle more than 31 bits when the value is higher than 2147483647 (Int8).

It does not work for **Integer** nor with **Long** datatype on the tag.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/c/9c67e221342647d4cd1e188aa3369bc460fd573b.png)  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/9/49d68429096a862e545996426c48be144315006c.png)

**Here is the UDT example**

```python
{
  "name": "BooleanArrayAlarm",
  "parameters": {
    "Tag": {
      "dataType": "String"
    }
  },
  "tagType": "UdtType",
  "tags": [
    {
      "opcItemPath": {
        "bindType": "parameter",
        "binding": "{Tag}"
      },
      "valueSource": "memory",
      "dataType": "BooleanArray",
      "name": "BooleanArray",
      "value": [
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false
      ],
      "tagType": "AtomicTag",
      "opcServer": "Ignition OPC UA Server"
    },
    {
      "valueSource": "expr",
      "expression": "binEnc(\r\n\t{[.]BooleanArray}[0],\r\n\t{[.]BooleanArray}[1],\r\n\t{[.]BooleanArray}[2],\r\n\t{[.]BooleanArray}[3],\r\n\t{[.]BooleanArray}[4],\r\n\t{[.]BooleanArray}[5],\r\n\t{[.]BooleanArray}[6],\r\n\t{[.]BooleanArray}[7],\r\n\t{[.]BooleanArray}[8],\r\n\t{[.]BooleanArray}[9],\r\n\t{[.]BooleanArray}[10],\r\n\t{[.]BooleanArray}[11],\r\n\t{[.]BooleanArray}[12],\r\n\t{[.]BooleanArray}[13],\r\n\t{[.]BooleanArray}[14],\r\n\t{[.]BooleanArray}[15],\r\n\t{[.]BooleanArray}[16],\r\n\t{[.]BooleanArray}[17],\r\n\t{[.]BooleanArray}[18],\r\n\t{[.]BooleanArray}[19],\r\n\t{[.]BooleanArray}[20],\r\n\t{[.]BooleanArray}[21],\r\n\t{[.]BooleanArray}[22],\r\n\t{[.]BooleanArray}[23],\r\n\t{[.]BooleanArray}[24],\r\n\t{[.]BooleanArray}[25],\r\n\t{[.]BooleanArray}[26],\r\n\t{[.]BooleanArray}[27],\r\n\t{[.]BooleanArray}[28],\r\n\t{[.]BooleanArray}[29],\r\n\t{[.]BooleanArray}[30],\r\n\t{[.]BooleanArray}[31]\r\n)\r\n",
      "dataType": "Int4",
      "name": "Integer",
      "tagType": "AtomicTag"
    }
  ]
}

```

---

<div class="post-metadata">

**Author:** ![michael.flagler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/michael.flagler/32/26272_2.png) [@michael.flagler](https://forum.inductiveautomation.com/u/michael.flagler)\
**Post date:** [June 19, 2024, 1:56pm UTC](https://forum.inductiveautomation.com/t/binenc-expression-function-not-working-as-documented/92090/2 "2024-06-19T13:56:14Z")

</div>

Looks like it's working to me. all bits on = -1 when using a signed integer which is what you have.

---

<div class="post-metadata">

**Author:** ![Starlyn\_Olivero](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@Starlyn\_Olivero](https://forum.inductiveautomation.com/u/Starlyn_Olivero)\
**Post date:** [June 19, 2024, 2:31pm UTC](https://forum.inductiveautomation.com/t/binenc-expression-function-not-working-as-documented/92090/3 "2024-06-19T14:31:21Z")

</div>

After testing the alarm on bit 32 it does work as expected. Thanks for clarifying.

---

<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:** [June 19, 2024, 2:35pm UTC](https://forum.inductiveautomation.com/t/binenc-expression-function-not-working-as-documented/92090/4 "2024-06-19T14:35:27Z")

</div>

Also, remember that `binEnc()` also swaps the order so that the Most Significant bit is the last argument, as opposed to the first.

So `binEnc(0,0,1,0)` results in 4, and not 2 as you might expect.

---

<div class="post-metadata">

**Author:** ![michael.flagler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/michael.flagler/32/26272_2.png) [@michael.flagler](https://forum.inductiveautomation.com/u/michael.flagler)\
**Post date:** [June 19, 2024, 2:36pm UTC](https://forum.inductiveautomation.com/t/binenc-expression-function-not-working-as-documented/92090/5 "2024-06-19T14:36:57Z")

</div>

Personally, it works as I would expect, as I wouldn't think nor want the first argument to be bit 31.

---

<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:** [June 19, 2024, 2:39pm UTC](https://forum.inductiveautomation.com/t/binenc-expression-function-not-working-as-documented/92090/6 "2024-06-19T14:39:41Z")

</div>

I don't disagree, but I've had to answer many questions about why something wasn't the number expected when it was because they were reading the values as a traditionally printed binary number, instead of reversed.

Just a note because it can really trip some people up.

---

<div class="post-metadata">

**Author:** ![Starlyn\_Olivero](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@Starlyn\_Olivero](https://forum.inductiveautomation.com/u/Starlyn_Olivero)\
**Post date:** [June 20, 2024, 11:01am UTC](https://forum.inductiveautomation.com/t/binenc-expression-function-not-working-as-documented/92090/7 "2024-06-20T11:01:22Z")

</div>

Hey Michael,

I've tried to use the bit state mode on an alarm on an encoded Long value but it does not seem to work after the 32 bits. So far, Bit State Mode only works for 32 bits due to the signed value I guess.

Any thoughts?

---

<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:** [June 20, 2024, 11:59am UTC](https://forum.inductiveautomation.com/t/binenc-expression-function-not-working-as-documented/92090/8 "2024-06-20T11:59:14Z")

</div>

The manual states that `binEnc()` will accept up to 64 arguments.

What does "does not seem to work" actually mean?

What result did you expect and what result did you actually get?

---

<div class="post-metadata">

**Author:** ![michael.flagler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/michael.flagler/32/26272_2.png) [@michael.flagler](https://forum.inductiveautomation.com/u/michael.flagler)\
**Post date:** [June 20, 2024, 12:12pm UTC](https://forum.inductiveautomation.com/t/binenc-expression-function-not-working-as-documented/92090/9 "2024-06-20T12:12:09Z")

</div>

In the documentation it says bit state works with integers. While I didn't see it explicitly call out a 32 bit integer, the integer datatype is only 32 bits.

> **[Tag Alarm Properties | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/platform/tags/tag-properties/tag-alarm-properties#alarm-mode-properties)**
>
> In 8.1.17, the Tag Editor was redesigned to improve usability. The new Tag Editor now requires fewer clicks and keeps relevant tag information visible while modifying bindings, alarms, and event scripts.

---

<div class="post-metadata">

**Author:** ![Starlyn\_Olivero](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@Starlyn\_Olivero](https://forum.inductiveautomation.com/u/Starlyn_Olivero)\
**Post date:** [June 20, 2024, 12:31pm UTC](https://forum.inductiveautomation.com/t/binenc-expression-function-not-working-as-documented/92090/10 "2024-06-20T12:31:27Z")

</div>

Yeah, I think this is the key info here. While binEnc() can take up to 64 arguments. For alarm handling one can only evaluate 32 bits hence limiting the great functionality of encoding 64bits.

@lrose if you try to evaluate above 32 bits on an alarm using bit state mode it won't trigger the alarm.

Thanks both

---

<div class="post-metadata">

**Author:** ![michael.flagler](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/michael.flagler/32/26272_2.png) [@michael.flagler](https://forum.inductiveautomation.com/u/michael.flagler)\
**Post date:** [June 20, 2024, 2:13pm UTC](https://forum.inductiveautomation.com/t/binenc-expression-function-not-working-as-documented/92090/11 "2024-06-20T14:13:06Z")

</div>

I've never needed 64-bits for my alarms, plus I do all of my alarm bit packing in the PLC when I do use it, and even though the PLC supports 64-bit, I only use 32-bit integers because that's all I've ever needed. In your case I would just split out the bits to 2 separate 32-bit integers (or why not just put the alarms directly on the tags you're bit packing and skip the bit packing all together).

Edit: Although if your data is an actual array, I don't know that you'll be able to alarm on the individual elements, but didn't know if that was just an example you were using for this post.

---

<div class="post-metadata">

**Author:** ![Starlyn\_Olivero](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@Starlyn\_Olivero](https://forum.inductiveautomation.com/u/Starlyn_Olivero)\
**Post date:** [June 20, 2024, 2:50pm UTC](https://forum.inductiveautomation.com/t/binenc-expression-function-not-working-as-documented/92090/12 "2024-06-20T14:50:34Z")

</div>

I love Ignition's flexibility.

2 separate 32-bit integers work but given the current state of the PLC code with many arrays of different lengths, I decided to go for a UDT with individual reference tags, each with one alarm. It allows me to script any array from an alarm list quickly. I am working with someone else's code.
