iX Talk
http://beijerinc.com/support/ix/forum/

Toggle specific bit in 32 bits variable
http://beijerinc.com/support/ix/forum/viewtopic.php?f=6&t=2117
Page 1 of 1

Author:  DeFacto [ Tue Aug 23, 2016 12:26 am ]
Post subject:  Toggle specific bit in 32 bits variable

Hello,

To read a specific bit in variable - expression is used, but is there a way to toggle specific bit in variable?

Author:  Chris D [ Wed Sep 07, 2016 12:51 pm ]
Post subject:  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

Author:  AMitchneck [ Thu Sep 08, 2016 4:37 pm ]
Post subject:  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).

Author:  DeFacto [ Wed Sep 28, 2016 12:54 pm ]
Post subject:  Re: Toggle specific bit in 32 bits variable

Thank you for the replies.

Page 1 of 1 All times are UTC - 7 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/