# Working with individual bits of an Integer tag

what is the best way (easiest/cleanest) to work with indivudual bits of an Integer tag?
i have an integer tag that i need to be able to both set and readback the individual bits.
i plan on using a checkbox to set/clear the bits.
Thanks,

Hi Scott

I think the best way is to use the getBit(number, position) in the expression functions.
“This function returns the bit value (an integer, 0 or 1) in the number at position position, according to its binary representation. The least significant bit in a number is position 0.” --> Manual --> Expression funtions–>Logic

So, create new DB Tags using that expression, one tag each bit from the integer that you wanna use

Example: I created DB Tags Q0,Q1, … Q7

Q0: 0000 0001 = getBit({(myInteger=0)}, 0) —> Q0= TRUE , Q1= FALSE , … , Q7= FALSE
Q1: 0000 0010 = getBit({(myInteger=2)}, 1) —> Q0= FALSE , Q1= TRUE , … , Q7= FALSE

Q7: 1000 0000 = getBit({(myInteger=128)}, 7) —> Q0= FALSE , Q1= FALSE , … , Q7= TRUE

For reading the individual bits I am using “bitwise &” which works well and seems a bit simpler.
here is an example:
({IntegerTagHere}&1) = Returns true if first bit is High
({IntegerTagHere}&2) = Returns true if 2nd bit is High, etc.

My bigger problem is how to SET individual bits of an Integer tag?
i would like to toggle individual bits using a checkbox component.
Thanks,

If you are writing to each bit individually you should really make a separate tag for each bit in SQLTags. What driver are you using? For example, on a SLC device you can address a bit like this:

[DeviceName]N7:0.1

where the .x is the bit number. Ignition will take care of reading and writing to the bit. If you don’t do that you have to do something special in Ignition like talked about in this forum post.