Page 1 of 1
Toggle specific bit in 32 bits variable
Posted: Tue Aug 23, 2016 12:26 am
by DeFacto
Hello,
To read a specific bit in variable - expression is used, but is there a way to toggle specific bit in variable?
Re: Toggle specific bit in 32 bits variable
Posted: Wed Sep 07, 2016 12:51 pm
by Chris D
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
Re: Toggle specific bit in 32 bits variable
Posted: Thu Sep 08, 2016 4:37 pm
by AMitchneck
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).
Re: Toggle specific bit in 32 bits variable
Posted: Wed Sep 28, 2016 12:54 pm
by DeFacto
Thank you for the replies.