Click here to Skip to main content
15,878,809 members
Articles / Programming Languages / C#
Tip/Trick

To Toggle or Not to Toggle - xor is Your Friend

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
12 Jan 2013CPOL1 min read 19.3K   4   6
Short tip to show a convenient use of the lesser known xor operator

Introduction

I struggle once in a while over some complicated if-elseif-else constructs that have a more concise alternative: use of the xor operator (means: exclusive-or) that many languages provide.

This tip shows the use of xor on an example of a conditional toggle function. Such functions may be used in automated GUI tests where e.g. in a tree view a sub-tree has to be opened if it is not yet open, or has to be closed if it is open.

It is a trivial tip, but it seems that many people either don't know of it or it does not occur to them to use it.

Using the Code

Assuming you had to write the following function:

C#
// sets the ToggleButton to the defined pressed state (true = pressed, false = not pressed).
void ClickIfNeeded(ToggleButton button, bool pressed)
{
   ...
}

The xor implementation provides in my view a cleaner implementation compared to the naive implementation:

Naive implementationxor based implementation
C#
void ClickIfNeeded(ToggleButton button,
                   bool pressed)
{
   if (button.IsPressed)
   {
      if (!pressed)
      {
          button.Click();
      }
   }
   else
   {
      if (pressed)
      {
          button.Click();
      }
   }
}
C#
void ClickIfNeeded(ToggleButton button,
                     bool pressed)
{
   // click the button if it has not yet
   // the requested pressed state
   if (button.IsPressed ^ pressed) button.Click();
}

The Exclusive-Or operator:

  • the xor operator has for C, C++, C#, etc. the following sign: ^
  • xor gives true if exactly one of the operands is true (hence: exclusive or)
  • xor gives false if both operands have the same value.

i.e. in the example above: only click the toggle button if it has not yet the requested pressed state.

History

V1.02013-01-12Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder eXternSoft GmbH
Switzerland Switzerland
I feel comfortable on a variety of systems (UNIX, Windows, cross-compiled embedded systems, etc.) in a variety of languages, environments, and tools.
I have a particular affinity to computer language analysis, testing, as well as quality management.

More information about what I do for a living can be found at my LinkedIn Profile and on my company's web page (German only).

Comments and Discussions

 
GeneralMy vote of 5 Pin
JamesHollowell16-Jan-13 18:43
JamesHollowell16-Jan-13 18:43 
Thanks for this tip!
It certainly cleaned up your code sample a bunch.
James Hollowell

GeneralRe: My vote of 5 Pin
Andreas Gieriet16-Jan-13 19:48
professionalAndreas Gieriet16-Jan-13 19:48 
GeneralMy vote of 5 Pin
arid0813-Jan-13 23:04
arid0813-Jan-13 23:04 
GeneralRe: My vote of 5 Pin
Andreas Gieriet14-Jan-13 1:56
professionalAndreas Gieriet14-Jan-13 1:56 
Questiongood idea but... Pin
Boombuler13-Jan-13 20:37
Boombuler13-Jan-13 20:37 
AnswerRe: good idea but... Pin
Andreas Gieriet14-Jan-13 1:55
professionalAndreas Gieriet14-Jan-13 1:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.