# Expression to convert 2's complement to signed int

**URL:** <https://forum.inductiveautomation.com/t/expression-to-convert-2s-complement-to-signed-int/41181>\
**Category:** Ignition\
**Created:** [December 3, 2020, 5:27pm UTC](https://forum.inductiveautomation.com/t/expression-to-convert-2s-complement-to-signed-int/41181 "2020-12-03T17:27:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![scott.steele](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@scott.steele](https://forum.inductiveautomation.com/u/scott.steele)\
**Post date:** [December 3, 2020, 5:27pm UTC](https://forum.inductiveautomation.com/t/expression-to-convert-2s-complement-to-signed-int/41181/1 "2020-12-03T17:27:33Z")

</div>

I’m using Ignition to collect data from a GE Multilin 850 and some of the register values are stored in 2’s complement instead of signed int. I created an expression tag to convert the OPC tag value to signed int. Below is what I came up with for an 8 bit register, is there an easier way to convert 2’s complement?

// Example: -12.34 stored as -1234 i.e. 64302

// register 1 byte 1:  
// register 1 byte 2:

//conversion of 2’s complement. 16th bit will identify if the number is negative or not.

if ((getBit({[.]Main\_Pwr1\_PF\_2sComp},7)), //change position 15 to 7 when using 8 bit register  
//if first bit is true, then flip all bits, subtract 1 and add a negative.  
toInt((binEnc(  
if (getBit({[.]Main\_Pwr1\_PF\_2sComp},0),0,1), //invert the bit value  
if (getBit({[.]Main\_Pwr1\_PF\_2sComp},1),0,1), //invert the bit value  
if (getBit({[.]Main\_Pwr1\_PF\_2sComp},2),0,1), //invert the bit value  
if (getBit({[.]Main\_Pwr1\_PF\_2sComp},3),0,1), //invert the bit value  
if (getBit({[.]Main\_Pwr1\_PF\_2sComp},4),0,1), //invert the bit value  
if (getBit({[.]Main\_Pwr1\_PF\_2sComp},5),0,1), //invert the bit value  
if (getBit({[.]Main\_Pwr1\_PF\_2sComp},6),0,1), //invert the bit value  
if (getBit({[.]Main\_Pwr1\_PF\_2sComp},7),0,1) //invert the bit value  
)-1)\*(-1)), //subtract one and add a negative to the number

```
    //if first bit is false, then convert to decimal number
	toInt(binEnc(
            getBit({[.]Main_Pwr1_PF_2sComp},0),
            getBit({[.]Main_Pwr1_PF_2sComp},1),
            getBit({[.]Main_Pwr1_PF_2sComp},2),
            getBit({[.]Main_Pwr1_PF_2sComp},3),
            getBit({[.]Main_Pwr1_PF_2sComp},4),
            getBit({[.]Main_Pwr1_PF_2sComp},5),
            getBit({[.]Main_Pwr1_PF_2sComp},6),
            getBit({[.]Main_Pwr1_PF_2sComp},7)
            )
            )
)
```

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [December 3, 2020, 5:33pm UTC](https://forum.inductiveautomation.com/t/expression-to-convert-2s-complement-to-signed-int/41181/2 "2020-12-03T17:33:57Z")

</div>

Can you try to better describe what you’re doing and provide an example of what the data looks like and why it’s wrong?

Two’s complement _is_ the default representation for signed integer values so I’m really not sure what you’re trying to describe here.

---

<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:** [December 3, 2020, 5:43pm UTC](https://forum.inductiveautomation.com/t/expression-to-convert-2s-complement-to-signed-int/41181/3 "2020-12-03T17:43:03Z")

</div>

Modbus, I presume (been a while since I looked a a MultiLin) ? If you are reading with prefix “HRUS”, just use “HR” instead.

[https://docs.inductiveautomation.com/display/DOC81/Modbus+Addressing#ModbusAddressing-ManuallySpecifyEachAddress](https://docs.inductiveautomation.com/display/DOC81/Modbus+Addressing#ModbusAddressing-ManuallySpecifyEachAddress)

---

<div class="post-metadata">

**Author:** ![scott.steele](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@scott.steele](https://forum.inductiveautomation.com/u/scott.steele)\
**Post date:** [December 3, 2020, 6:16pm UTC](https://forum.inductiveautomation.com/t/expression-to-convert-2s-complement-to-signed-int/41181/4 "2020-12-03T18:16:52Z")

</div>

Well I guess I didn’t understand how signed integers are stored in the first place. Are all signed int stored as 2’s complement?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [December 3, 2020, 6:17pm UTC](https://forum.inductiveautomation.com/t/expression-to-convert-2s-complement-to-signed-int/41181/5 "2020-12-03T18:17:55Z")

</div>

Certainly in Ignition, Java in general, and that’s the assumption in every one of the drivers as well.

---

<div class="post-metadata">

**Author:** ![scott.steele](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@scott.steele](https://forum.inductiveautomation.com/u/scott.steele)\
**Post date:** [December 3, 2020, 6:21pm UTC](https://forum.inductiveautomation.com/t/expression-to-convert-2s-complement-to-signed-int/41181/6 "2020-12-03T18:21:24Z")

</div>

Thanks for clarifying, makes much more sense now.
