# How to: Inverting Bits

The guide demonstrates how to create an expression that will invert the bits of a given tag. Before starting, create a local Modbus device, a tag with the name *source\_tag* and data format WORD (16-bit unsigned integer).

Open *Monokot Server Administrator*, double-click *Scripts* on the *Server Explorer* pane and go to the *Modules* tab. Click the *New Module* button and name the module *BitsOp*. Insert the following code into the code editor:

```javascript
function bit_test(num, bit){
    return ((num>>bit) % 2 != 0)
}

function bit_set(num, bit){
    return num | 1<<bit;
}

function bit_clear(num, bit){
    return num & ~(1<<bit);
}

function bit_toggle(num, bit){
    return bit_test(num, bit) ? bit_clear(num, bit) : bit_set(num, bit);
}
```

Press *Ctrl + Enter* to apply the change in the code editor.

![](https://4282443477-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3cwznMbQcEQxtnjiRDYX%2Fuploads%2FJug7CROb0bqxIE7QJSsH%2FHow%20to_%20Inverting%20Bits%20\(360039888731\)_image-0.png?alt=media\&token=56f98a95-b953-4bef-8f72-f3e1cd7e033b)

Thus, the following functions are defined in the *BitsOp* module:

* **bit\_test:** returns the state (true or false) of the bit number *bit* from the number *num*
* **bit\_set:** sets the bit number *bit* of the specified number *num* to true and returns the resulting number
* **bit\_clear:** resets the bit number *bit* of the specified number *num* to false and returns the resulting number
* **bit\_toggle:** inverts the bit number *bit* of the specified number *num* and returns the resulting number

Go to the *Expressions* tab and click the *New Expression* button. For the expression that appeared, set the name *Inverse* and specify the data type WORD (16-bit unsigned integer). Insert the following code into the expression code editor:

```javascript
const NUMBER_OF_BIT = 16;

var value = context.data['source_tag_trig'].state.value;

for (var i = 0; i < NUMBER_OF_BIT; i++)
  value = bit_toggle(value, i);

return value;
```

Press *Ctrl + Enter* to apply the change in the code editor.

While in the code editor you can check the syntax by pressing the F9 key

Go to the *Triggers* tab and click *Add Data*.... Select the previously created tag (*source\_tag*) as the data source for the trigger. Enter *source\_tag\_trig* as the name (key) of the trigger.

![](https://4282443477-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3cwznMbQcEQxtnjiRDYX%2Fuploads%2FrB4lY0ut98l8E0Q0ZmjA%2FHow%20to_%20Inverting%20Bits%20\(360039888731\)_image-1.png?alt=media\&token=24959b04-3841-4108-9253-afc5c9221cbb)

&#x20;Go to the *Imports* tab and click the checkbox of the *BitsOp* module.

![](https://4282443477-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3cwznMbQcEQxtnjiRDYX%2Fuploads%2FZuE270KUYWYA6zM4DqFE%2FHow%20to_%20Inverting%20Bits%20\(360039888731\)_image-2.png?alt=media\&token=addab954-ede1-4b17-86d3-2e9744ef99f3)

In order for the changes to take effect on the server, click the Sync button. If you switch to *Go Online* mode, you will see a picture similar to the following (in my case, the number *21845* is inverted).&#x20;

![](https://4282443477-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3cwznMbQcEQxtnjiRDYX%2Fuploads%2FPsJPjxDHcAwrYZIEsBVG%2FHow%20to_%20Inverting%20Bits%20\(360039888731\)_image-3.png?alt=media\&token=58277e03-041d-4845-8aea-de2653db449f)
