Bit Level Operations on INT32 or INT16

Discussion of application development using iX Developer, including but not limited to getting started, using the functions tab, properties, objects and installation.
Post Reply
GTUnit
Posts: 6
Joined: Sun Sep 28, 2014 1:03 am

Bit Level Operations on INT32 or INT16

Post by GTUnit »

I have a INT32 tag called ControlDINT
I need to Read and Write to the individual bits in that tag.

In Compactlogix processors I can address this as
ControlDINT.0
ControlDINT.1
ControlDINT.2
ControlDINT.3 and so on up to ControlDINT.31

How can I read and write these bits using the panel?

The reference and user documentation is not of any use.

AMitchneck
Posts: 137
Joined: Mon Jun 11, 2012 2:10 pm

Re: Bit Level Operations on INT32 or INT16

Post by AMitchneck »

To change a single bit you can write script such as

Code: Select all

int setBit(int currentValue, int bitNumber, bool value)
{
    int bit = 1 << bitNumber;
    if (value)
        currentValue |= bit;
    else
        currentValue &= ~bit;
    return currentValue;
}
Note that when you write to a tag the entire tag value will be written to the controller not just the single bit you set.
Adam M.
Controls Engineer
FlexEnergy

Gargy
Posts: 4
Joined: Thu Feb 05, 2015 3:51 am

Re: Bit Level Operations on INT32 or INT16

Post by Gargy »

Hello all!

'Cause I have 0 knowledge with C# and scripting I took another approach...

Each INT is a sum of numbers. Every bit has its own number. Bit 0 = 1, Bit 1 = 2... BIT 7 = 128.. And so on... If I add or subtract from INT a value of 2, then I will change only the BIT 1 in whole INT.

Example. I want to change BIT5 in the whole INT. This is decimal number 32. So I add two buttons - one is visible if BIT5 is 0 and one if BIT5 is 1. First button add 32 to the INT (Increment Analog) and the second subtract 32 (Decrement Analog). The end result is changing Bit 5 from 1 to 0 or in other direction.

I hope I helped someone...

BR
Gargy

Post Reply