Welcome Guest ( Login | Register )



All times are UTC - 7 hours [ DST ]



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Toggle specific bit in 32 bits variable
PostPosted: Tue Aug 23, 2016 12:26 am 

Joined: Mon Jul 07, 2014 11:38 pm
Posts: 2

Offline
Hello,

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


 Profile  
 
 Post subject: Re: Toggle specific bit in 32 bits variable
PostPosted: Wed Sep 07, 2016 12:51 pm 

Joined: Tue Aug 05, 2014 10:53 am
Posts: 6

Offline
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


 Profile  
 
 Post subject: Re: Toggle specific bit in 32 bits variable
PostPosted: Thu Sep 08, 2016 4:37 pm 

Joined: Mon Jun 11, 2012 2:10 pm
Posts: 97

Offline
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).

_________________
Adam M.
Controls Engineer
FlexEnergy


 Profile  
 
 Post subject: Re: Toggle specific bit in 32 bits variable
PostPosted: Wed Sep 28, 2016 12:54 pm 

Joined: Mon Jul 07, 2014 11:38 pm
Posts: 2

Offline
Thank you for the replies.


 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 7 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: