473,500 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Quick way to toggle a bit?

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

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

Nov 15 '05 #1
6 20043
"Michael A. Covington"
<lo**@www.covingtoninnovations.com.for.address> wrote in
news:e2**************@TK2MSFTNGP12.phx.gbl:
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.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 15 '05 #2
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);
true

"Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in message
news:Xn**********************************@207.46.2 48.16... "Michael A. Covington"
<lo**@www.covingtoninnovations.com.for.address> wrote in
news:e2**************@TK2MSFTNGP12.phx.gbl:
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.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/

Nov 15 '05 #3

"Michael A. Covington" <lo**@www.covingtoninnovations.com.for.address> wrote
in message news:e2****************@TK2MSFTNGP12.phx.gbl...
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.
Nov 15 '05 #4
Michael A. Covington <lo**@www.covingtoninnovations.com.for.address>
wrote:
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.


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Michael A. Covington <lo**@www.covingtoninnovations.com.for.address>
wrote:
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.


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! :)

--
Michael Covington, Ph.D.
Associate Director
Artificial Intelligence Center
The University of Georgia
Nov 15 '05 #6
"john bailo" <jb****@vestcom.com> wrote in
news:ff******************************@news.teranew s.com:
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);
true


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.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2267
by: leegold2 | last post by:
I wondered if anyone would give me code- I think it would be easy, but I'm a complete newbie. What I want to do is to show many tables in a brief truncated format and then for each table offer the...
11
4781
by: MLH | last post by:
Why is that? If I choose the tiny check boxes which are hard to hit with a mouse, it works fine. But option buttions, shich can be sized big enough for people with limited sight and dexterity...
4
4329
by: Neil Coleclough | last post by:
I am constructing a database to process product returns for my Company. I have a number of toggle buttons to identify the stage to which each return has been processed. For example, clicking the...
5
3424
by: Steven | last post by:
Can anyone tell me how to toggle the "Caps Lock" key? Thanks in advance
50
28226
by: sksoule | last post by:
Hi, Can anyone please tell me how to toggle a nth bit. For example, I am having a variable which is of 8 bits. I want to toggle the nth bit. Regards, Santosh
1
5218
by: barbalu | last post by:
Hello. I'm painfully new to this and am having trouble figuring out what I'm doing wrong... I have two small tables in a simple html file. I want each table to have the ability to collapse/expand...
3
3792
by: Killer42 | last post by:
Hi all. I have a frame with some toggle buttons in it. All is working fine, except that the user can't tell which option is selected to begin with. How do I set one of the toggle buttons to the...
1
1937
by: swiftouch | last post by:
I'm getting an error message in FF2.0: document.getElementById(toggle) has no properties The goal of the script is, when I hover my mouse over an image, to make one div element visible while...
3
2667
ilya Kraft
by: ilya Kraft | last post by:
Hello, Quick problem here. On my website I have a button. When user clicks on the button it expands div. you can se how it works here http://inelmo.com/noluck/ As you can see div expands...
0
7182
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7232
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5490
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4923
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4611
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3106
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1430
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
672
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
316
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.