Quick way to toggle a bit?

  • Thread starter Thread starter Michael A. Covington
  • Start date Start date
M

Michael A. Covington

Is there a quick way to toggle a Boolean variable? Something shorter than:

blah.blither.blatt = ! blah.blither.blatt;
 
Is there a quick way to toggle a Boolean variable? Something
shorter than:

blah.blither.blatt = ! blah.blither.blatt;

Michael,

I'm not aware of any C# language construct that could shorten that
line of code. A common idiom to shorten object references is to grab
a reference to the object:

Blither b = blah.blither;
b.blatt = !b.blatt;

Or, this may be quicker to type, but I suspect it's a solution in
search of a problem:

void procedure ToggleBoolean(
ref bool b)
{
b = !b;
}

...

ToggleBoolean(ref blah.blither.blatt);


Chris.
 
couldn't you override an operator to do what you want.
and create a unary boolean switch?

so, in the way the increment operator is +=
the boolean flip would be !,
so

myBool = false;
!myBool;
Console.WriteLine(myBool.ToString);
 
Michael A. Covington said:
Is there a quick way to toggle a Boolean variable? Something shorter than:

blah.blither.blatt = ! blah.blither.blatt;


Found it. I can write:


blah.blither.blatt ^= true;


which flips the Boolean value, because it is equivalent to:


blah.blither.blatt = blah.blither.blatt ^ true;


where ^ is xor.
 
Michael A. Covington said:
Found it. I can write:


blah.blither.blatt ^= true;


which flips the Boolean value, because it is equivalent to:


blah.blither.blatt = blah.blither.blatt ^ true;


where ^ is xor.

While I'm sure you're right about it working, I don't think that's an
argument to use it - at least not if you think anyone else is going to
read your code. Even though it's fewer characters than

blah.blither.blatt = !blah.blither.blatt;

I would suggest that most people would find the latter far
easier/quicker to understand - and code generally spends more time in
maintenance than being written. If you're that keen on saving space at
the expense of readability, you could just shorten your variable names
and stop using spaces:

a.b.c!=a.b.c;

is shorter than

blah.blither.blatt ^= true;

anyway.

Of course, if this is all just an academic exercise for interest's
sake, that's a different matter.
 
Jon Skeet said:
While I'm sure you're right about it working, I don't think that's an
argument to use it - at least not if you think anyone else is going to
read your code. Even though it's fewer characters than

blah.blither.blatt = !blah.blither.blatt;

I would suggest that most people would find the latter far
easier/quicker to understand - and code generally spends more time in
maintenance than being written.

I had the same thought, actually. People are going to wonder what " ^=
true " does, and they're going to misread it as " = true ".
anyway.

Of course, if this is all just an academic exercise for interest's
sake, that's a different matter.

Academic is what I am! :)
 
couldn't you override an operator to do what you want.
and create a unary boolean switch?

so, in the way the increment operator is +=
the boolean flip would be !,
so

myBool = false;
!myBool;
Console.WriteLine(myBool.ToString);

John,

You could define a new behavior for the unary ! operator in a user-
defined class or struct. However, the behavior of the built-in
System.Boolean (bool) type's unary ! operator cannot be changed.


Chris.
 
Back
Top