Hello,
To read a specific bit in variable - expression is used, but is there a way to toggle specific bit in variable?
Toggle specific bit in 32 bits variable
Re: Toggle specific bit in 32 bits variable
Hi,
you can use script and the XOR operator (^)
For example:
Gobals.Tags.myvar.Value = Gobals.Tags.myvar.Value ^ (1<< 5);
to toggle bit 5.
regards
Chris
you can use script and the XOR operator (^)
For example:
Gobals.Tags.myvar.Value = Gobals.Tags.myvar.Value ^ (1<< 5);
to toggle bit 5.
regards
Chris
-
- Posts: 137
- Joined: Mon Jun 11, 2012 2:10 pm
Re: Toggle specific bit in 32 bits variable
FYI, another way of doing Chris D's suggestion:
Globals.Tags.myvar.Value ^= 1 << 5;
this code also toggles bit 5 using and exclusive-or. One warning I will give: this method reads what current bits are set, toggles the one bit, then writes all bits back. If other bits are changed in the mean time by the controller or whatever that change will be lost (or improperly translated as a toggle depending on how you are using them).
Globals.Tags.myvar.Value ^= 1 << 5;
this code also toggles bit 5 using and exclusive-or. One warning I will give: this method reads what current bits are set, toggles the one bit, then writes all bits back. If other bits are changed in the mean time by the controller or whatever that change will be lost (or improperly translated as a toggle depending on how you are using them).
Adam M.
Controls Engineer
FlexEnergy
Controls Engineer
FlexEnergy
Re: Toggle specific bit in 32 bits variable
Thank you for the replies.