Official Everybody Edits Forums

Do you think I could just leave this part blank and it'd be okay? We're just going to replace the whole thing with a header image anyway, right?

You are not logged in.

#1 Before February 2015

Koya
Fabulous Member
From: The island with those Brits
Joined: 2015-02-18
Posts: 6,310

[Guide] Switching a bool

I've seen a couple examples of turning true to false and false to true which are more complicated than they need to be, such as

if (Countdown.Enabled) {     Countdown.Enabled = false; } else {     Countdown.Enabled = true; }

Simply use

Countdown.Enabled = !Countdown.Enabled;

Wait, no - use

Flish wrote:

[boolean] ^= true;

Last edited by Metatron (Aug 17 2014 7:39:25 pm)


Po9cnQh.png

PLNQVL8.png
Thank you eleizibeth ^

1SYOldu.png

I stack my signatures rather than delete them so I don't lose them
giphy.gif

WfSi4mm.png

Offline

#2 Before February 2015

Zumza
Member
From: root
Joined: 2015-02-17
Posts: 4,645

Re: [Guide] Switching a bool

Squad wrote:

I've seen a couple examples of turning true to false and false to true which are more complicated than they need to be, such as

if (Countdown.Enabled) {     Countdown.Enabled = false; } else {     Countdown.Enabled = true; }

Nope never seen this...
But I used to:

If(Countdown.Enabled)  Countdown.Enabled = false; else  Countdown.Enabled = true;

Less usage of RAM...

Last edited by The Doctor (Aug 14 2014 11:58:49 am)


Everybody edits, but some edit more than others

Offline

#3 Before February 2015

TiKen
Member
Joined: 2015-02-24
Posts: 298

Re: [Guide] Switching a bool

I don't think that 1 or two bits will kill your computer... x = !x is way smaller, and way classier ^^"

Offline

#4 Before February 2015

Zumza
Member
From: root
Joined: 2015-02-17
Posts: 4,645

Re: [Guide] Switching a bool

If you don't have much RAM it matters!

Last edited by The Doctor (Aug 14 2014 12:48:34 pm)


Everybody edits, but some edit more than others

Offline

#5 Before February 2015

TiKen
Member
Joined: 2015-02-24
Posts: 298

Re: [Guide] Switching a bool

The Doctor wrote:

If you don't have much RAM it matters!

Well, if you're coding on a commodore, sure. But for instance windows 8 needs 1GB of RAM and a regular PC as 4GB. Which is roughly (4-1)*10^7 bits "available". So as I said, 1 or 2 bits won't change a thing.

Offline

#6 Before February 2015

Koya
Fabulous Member
From: The island with those Brits
Joined: 2015-02-18
Posts: 6,310

Re: [Guide] Switching a bool

I'm talking more about clean coding rather than making it easier for your computer (unless you have thousands of these)


Po9cnQh.png

PLNQVL8.png
Thank you eleizibeth ^

1SYOldu.png

I stack my signatures rather than delete them so I don't lose them
giphy.gif

WfSi4mm.png

Offline

#7 Before February 2015

Hexagon
Member
Joined: 2015-04-22
Posts: 1,213

Re: [Guide] Switching a bool

A smart compiler should optimize this anyway, so it should be a matter of preference. The performance savings are very very minimal if anything noticeable. However var = !var is a very clean way to express it.

Offline

#8 Before February 2015

lrussell
Member
From: Saturn's Titan
Joined: 2015-02-15
Posts: 843
Website

Re: [Guide] Switching a bool

You all forgot about:

bool a = (b) ? false : true;

All in one line, (should be) same as if statement.
Much many happier. //forums.everybodyedits.com/img/smilies/wink

Offline

#9 Before February 2015

ugotpwned
Member
Joined: 2015-02-16
Posts: 376

Re: [Guide] Switching a bool

lrussell wrote:

You all forgot about:

bool a = (b) ? false : true;

All in one line, (should be) same as if statement.
Much many happier. //forums.everybodyedits.com/img/smilies/wink

bool a = b ? false : true;

2 less chars.

Offline

#10 Before February 2015

Zumza
Member
From: root
Joined: 2015-02-17
Posts: 4,645

Re: [Guide] Switching a bool

tikenalpha wrote:
The Doctor wrote:

If you don't have much RAM it matters!

Well, if you're coding on a commodore, sure. But for instance windows 8 needs 1GB of RAM and a regular PC as 4GB. Which is roughly (4-1)*10^7 bits "available". So as I said, 1 or 2 bits won't change a thing.

We'll I got 500MB.... And yes surprising I can run Win. 8! But I use linux most times.

Also the negation takes time as if. And if you use that in C you can have troubles.

Last edited by The Doctor (Aug 15 2014 1:01:30 am)


Everybody edits, but some edit more than others

Offline

#11 Before February 2015

Hexagon
Member
Joined: 2015-04-22
Posts: 1,213

Re: [Guide] Switching a bool

What about nothing (0 chars)? For example, whenever you need to use the value in some way, simply invert the if statement.

Offline

#12 Before February 2015

TiKen
Member
Joined: 2015-02-24
Posts: 298

Re: [Guide] Switching a bool

The Doctor wrote:
tikenalpha wrote:
The Doctor wrote:

If you don't have much RAM it matters!

Well, if you're coding on a commodore, sure. But for instance windows 8 needs 1GB of RAM and a regular PC as 4GB. Which is roughly (4-1)*10^7 bits "available". So as I said, 1 or 2 bits won't change a thing.

We'll I got 500MB.... And yes surprising I can run Win. 8! But I use linux most times.

Also the negation takes time as if. And if you use that in C you can have troubles.

So it should leave you around 200MB to work other stuff. You could still do 100 000 000*8 "x = !x" at the same time. Btw, made a huge mistake in my precedent post. 4GB gives you (4-1)*10^10 bits "available".

As for C, the boolean doesn't exist, so yeah, you will have a problem. So instead, you'll use "x = 1 - x".

Offline

#13 Before February 2015

ZeldaXD
EE Homeboy
From: Cyprus
Joined: 2015-02-15
Posts: 1,539
Website

Re: [Guide] Switching a bool

Wtf kind of potato machine do you use zumza


gLjTZE1.png

Offline

Wooted by:

#14 Before February 2015

Flish
Guest

Re: [Guide] Switching a bool

Couldn't you use:
[boolean] ^= true;

#15 Before February 2015

Koya
Fabulous Member
From: The island with those Brits
Joined: 2015-02-18
Posts: 6,310

Re: [Guide] Switching a bool

Flish wrote:

Couldn't you use:
[boolean] ^= true;

I didn't even realise that existed - thank you so much for bringing this up.


Po9cnQh.png

PLNQVL8.png
Thank you eleizibeth ^

1SYOldu.png

I stack my signatures rather than delete them so I don't lose them
giphy.gif

WfSi4mm.png

Offline

#16 Before February 2015

Flish
Guest

Re: [Guide] Switching a bool

Squad wrote:
Flish wrote:

Couldn't you use:
[boolean] ^= true;

I didn't even realise that existed - thank you so much for bringing this up.

Not a problem. I'm a noob at C#, so I am still learning. I came across that method of toggling bools quite awhile ago.

#17 Before February 2015

TiKen
Member
Joined: 2015-02-24
Posts: 298

Re: [Guide] Switching a bool

Flish wrote:

Couldn't you use:
[boolean] ^= true;

You could (I didn't know this one, I had to check ^^) but I'm not sure it's quicker. Probably slower I think (you won't see it, unless you're using an amstrad with Windows Vista on it).

Offline

#18 Before February 2015

Zumza
Member
From: root
Joined: 2015-02-17
Posts: 4,645

Re: [Guide] Switching a bool

ZeldaXD wrote:

Wtf kind of potato machine do you use zumza

Asus potato //forums.everybodyedits.com/img/smilies/tongue


Everybody edits, but some edit more than others

Offline

#19 Before February 2015

TiKen
Member
Joined: 2015-02-24
Posts: 298

Re: [Guide] Switching a bool

The Doctor wrote:

Asus potato //forums.everybodyedits.com/img/smilies/tongue

GLaDOS is running on a potato, why are you complaining about RAM ? //forums.everybodyedits.com/img/smilies/big_smile

Offline

Wooted by:

#20 Before February 2015

Kaslai
Official Caroler
From: SEAͩT̓͑TLͯͥͧͪ̽ͧE͑̚
Joined: 2015-02-17
Posts: 787

Re: [Guide] Switching a bool

In the end, any approach you take will likely result in the exact same code generated by your compiler.

I'd also like to point out that in any language that is compiled to any extent, whitespace and brackets have no impact whatsoever on execution time (provided that it's still syntactically the same code). The only thing you'll save is a few bytes of source code, but that really doesn't matter at all on any modern machine.

Readability, maintainability, and correctness are the most important aspects of code. Everything else is secondary. Many modern optimizing compilers are so good that they can actually do some magic with loop reordering to turn naive O(n^2) algorithms into O(n) or even O(log(n)) algorithms.

...

And anyways, the most clear way to invert a boolean is

x = !x;

Offline

Koya1423758444202988

Board footer

Powered by FluxBB

[ Started around 1713490080.0242 - Generated in 0.086 seconds, 13 queries executed - Memory usage: 1.7 MiB (Peak: 1.94 MiB) ]