# Can I use base 2 (binary) numbers in the expression language?

**URL:** <https://forum.inductiveautomation.com/t/can-i-use-base-2-binary-numbers-in-the-expression-language/62384>\
**Category:** Ignition\
**Created:** [July 28, 2022, 4:36am UTC](https://forum.inductiveautomation.com/t/can-i-use-base-2-binary-numbers-in-the-expression-language/62384 "2022-07-28T04:36:34Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [July 28, 2022, 4:36am UTC](https://forum.inductiveautomation.com/t/can-i-use-base-2-binary-numbers-in-the-expression-language/62384/1 "2022-07-28T04:36:35Z")

</div>

if I use `binEnc` in a case, I’d like to use binary numbers instead of decimal in the case conditions to make it easier to read.

e.g.

```auto
case(binEnc(1,1,0)
   ,b000, ''
   ,b010, ''
   ,b100, ''
   ,b110, ''
   ,'Invalid'
)

```

Is this possible? I can’t find anything in the user manual…

I guess I could use:

```auto
case(binEnc(1,1,0)
   ,binEnc(0,0,0), ''
   ,binEnc(0,1,0), ''
   ,binEnc(1,0,0), ''
   ,binEnc(1,1,0), ''
   ,'Invalid'
)

```

but that’s a bit… yeah…  
I know some of you are computers and can just convert 53 to `110101`, but I’m not and prefer simple things like the metric system 🙂

---

<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:** [July 28, 2022, 2:24pm UTC](https://forum.inductiveautomation.com/t/can-i-use-base-2-binary-numbers-in-the-expression-language/62384/2 "2022-07-28T14:24:59Z")

</div>

Unfortunately, I don’t think that binary literals are accepted, although hex literals are.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/3/83c3d94f982dfdfe71f9f5f2983fe90b4999ffa4.png)

I’m not sure that’s more readable. It is less key strokes though. 😆

Also, be careful if you do go the route of `binEnc()`. I would look at `binEnc(1,0,0)` and think 4, but it’s actually computed as 1, so that could add confusion 🤷🏻‍♂️

---

<div class="post-metadata">

**Author:** ![KathyApplebaum](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kathyapplebaum/32/7261_2.png) [@KathyApplebaum](https://forum.inductiveautomation.com/u/KathyApplebaum)\
**Post date:** [July 28, 2022, 2:52pm UTC](https://forum.inductiveautomation.com/t/can-i-use-base-2-binary-numbers-in-the-expression-language/62384/3 "2022-07-28T14:52:47Z")

</div>

You could use

```auto
case(fromBinary("110")
   ,fromBinary("000"), ''
   ,fromBinary("010"), ''
   ,fromBinary("100"), ''
   ,fromBinary("110"), ''
   ,'Invalid'
)

```

---

<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:** [July 28, 2022, 3:15pm UTC](https://forum.inductiveautomation.com/t/can-i-use-base-2-binary-numbers-in-the-expression-language/62384/4 "2022-07-28T15:15:06Z")

</div>

Blegh. @nminchin , use Hex.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [July 29, 2022, 8:19am UTC](https://forum.inductiveautomation.com/t/can-i-use-base-2-binary-numbers-in-the-expression-language/62384/5 "2022-07-29T08:19:44Z")

</div>

I agree with Phil here. Hex would be my choice too.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [July 29, 2022, 8:56am UTC](https://forum.inductiveautomation.com/t/can-i-use-base-2-binary-numbers-in-the-expression-language/62384/6 "2022-07-29T08:56:07Z")

</div>

The whole point is to make it easy to see exactly what inputs you’re looking for though, hex is just a number and won’t identify bits that are on without calculation. It would be the exact same as using decimal

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [July 29, 2022, 9:46am UTC](https://forum.inductiveautomation.com/t/can-i-use-base-2-binary-numbers-in-the-expression-language/62384/7 "2022-07-29T09:46:36Z")

</div>

convert the number to bin with format for leading zeors if you wish

```auto
case(numberFormat(toBinary(3),"000")
   ,"000", '' //0
   ,"001", '' //1
   ,"010", '' //2
   ,"011", '' //3
   ,'Invalid'
)

```

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [July 29, 2022, 10:23am UTC](https://forum.inductiveautomation.com/t/can-i-use-base-2-binary-numbers-in-the-expression-language/62384/8 "2022-07-29T10:23:28Z")

</div>

The relationship between base 2 and base 16 for 8 bits bytes being what it is, it’s not exactly the same as decimal. Hex to binary mental conversion is relatively simple, though I agree it’s less ‘graphic’ than having the bits displayed directly.

That being said, you could maybe use hex/decimal and put the binary representation in a comment on the same line:

```auto
case(binEnc(1, 1, 0),
    0x0, "", // 0000
    0x1, "", // 0001
    0x2, "", // 0010
    0x3, "", // 0011
    "invalid"
)

```

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [July 29, 2022, 11:01am UTC](https://forum.inductiveautomation.com/t/can-i-use-base-2-binary-numbers-in-the-expression-language/62384/10 "2022-07-29T11:01:11Z")

</div>

Using hex, you only need to know it one nibble at a time (see what I did there?)

---

<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:** [July 29, 2022, 11:53am UTC](https://forum.inductiveautomation.com/t/can-i-use-base-2-binary-numbers-in-the-expression-language/62384/11 "2022-07-29T11:53:26Z")

</div>

> [@nminchin](#):
>
> won’t identify bits that are on without calculation

Hex perfectly aligns with four bits at a time. A little experience, and yes, you will "see" the bits at a glance.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [July 29, 2022, 5:53pm UTC](https://forum.inductiveautomation.com/t/can-i-use-base-2-binary-numbers-in-the-expression-language/62384/12 "2022-07-29T17:53:32Z")

</div>

Hey, I did this one 2 weeks ago !

---

<div class="post-metadata">

**Author:** ![Alistair](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@Alistair](https://forum.inductiveautomation.com/u/Alistair)\
**Post date:** [August 2, 2022, 8:42am UTC](https://forum.inductiveautomation.com/t/can-i-use-base-2-binary-numbers-in-the-expression-language/62384/13 "2022-08-02T08:42:53Z")

</div>

You’re over thinkin’ it. Keep it simple and put the detail in the comments, then the next guy will know exactly what you’re on about. 👍

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [August 2, 2022, 9:33am UTC](https://forum.inductiveautomation.com/t/can-i-use-base-2-binary-numbers-in-the-expression-language/62384/14 "2022-08-02T09:33:13Z")

</div>

I ended up going with binEnc for all, compare value and cases. Made it super easy to write and compare 🙂 you hex guys are all from another “base”😉
