Could someone explain to me what this does?

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
User avatar
Quzar
Dream Coder
Dream Coder
Posts: 7499
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Wed Jul 31, 2002 12:14 am
Location: Miami, FL
Has thanked: 4 times
Been thanked: 10 times
Contact:

Could someone explain to me what this does?

Post by Quzar »

Code: Select all

	conf.fullscreen=conf.fullscreen ? 0 : 1;
i think it says, if fullscreen is 0 then make it 1 and visa versa, but i have never seen this sort of operation before.
"When you post fewer lines of text than your signature, consider not posting at all." - A Wise Man
Rand Linden
bleemcast! Creator
bleemcast! Creator
Posts: 882
Joined: Wed Oct 17, 2001 7:44 pm
Location: Los Angeles, CA
Has thanked: 0
Been thanked: 0
Contact:

Post by Rand Linden »

You are correct.

Although, it would probably be clearer to use:

conf.fullscreen = (1-conf.fullscreen);

Of course, that assumes that the value is either 0 or 1 always, but you get the idea... there's a bunch of ways to write the toggle that are far easier to read -- many will break depending on the compiler and all sorts of other issues...

The construct you quoted is actually quite useful in all sorts of cases -- whether or not it's "cleaner" or "faster" to use it to toggle booleans will depend on the compiler and all sorts of other stuff.

Rand.

edit: I've usually heard of it as a "trinary" operation -- think "binary", but using tri... Do a google for "trinary c c++ language" and you'll find it easily.
Ex-Cyber
DCEmu User with No Life
DCEmu User with No Life
Posts: 3641
Joined: Sat Feb 16, 2002 1:55 pm
Has thanked: 0
Been thanked: 0

Post by Ex-Cyber »

It's also commonly spelled "ternary", for reasons that are clearly beyond my feeble grasp of linguistics. It's also sometimes called the "conditional operator".
"You know, I have a great, wonderful, really original method of teaching antitrust law, and it kept 80 percent of the students awake. They learned things. It was fabulous." -- Justice Stephen Breyer
ATani
DCGen Creator
DCGen Creator
Posts: 66
Joined: Sat Jan 19, 2002 12:54 am
Location: Near Yosemite, CA
Has thanked: 0
Been thanked: 0
Contact:

Post by ATani »

Rand Linden wrote:You are correct.

Although, it would probably be clearer to use:

conf.fullscreen = (1-conf.fullscreen);
Another common construct for this is:
conf.fullscreen = !conf.fullscreen;

Which depending on your compiler will do the same thing..

Mike
User avatar
Quzar
Dream Coder
Dream Coder
Posts: 7499
Joined: Wed Jul 31, 2002 12:14 am
Location: Miami, FL
Has thanked: 4 times
Been thanked: 10 times
Contact:

Post by Quzar »

Now, does anyone know which of these is the best solution? That is, not for readability, but for speed.
"When you post fewer lines of text than your signature, consider not posting at all." - A Wise Man
Rand Linden
bleemcast! Creator
bleemcast! Creator
Posts: 882
Joined: Wed Oct 17, 2001 7:44 pm
Location: Los Angeles, CA
Has thanked: 0
Been thanked: 0
Contact:

Post by Rand Linden »

ATani wrote:Another common construct for this is:
conf.fullscreen = !conf.fullscreen;

Which depending on your compiler will do the same thing..

Mike
Yes, that's correct also...

I was trying to avoid all the boolean vs. binary logic issues that arise with compilers and optimizations and all sorts of other crap...

... but feel free to expound upon all the inane nuances if you want! :-)

quzar: Chances are very (very) high that a toggle operation isn't where you need to be worrying about speed optimizations.

Rand.

edit: I've also heard of that type of construct as a "choice" operator, but that terminology is a bit misleading... but I'm adding this just incase you can't find sufficient search results using all the other stuff.
User avatar
Quzar
Dream Coder
Dream Coder
Posts: 7499
Joined: Wed Jul 31, 2002 12:14 am
Location: Miami, FL
Has thanked: 4 times
Been thanked: 10 times
Contact:

Post by Quzar »

ok, thanks for your help =).

one last question. Are there any other ternary/trinary operators? many of the references i have found simply say "the ternary operator".
"When you post fewer lines of text than your signature, consider not posting at all." - A Wise Man
BlackAura
DC Developer
DC Developer
Posts: 9951
Joined: Sun Dec 30, 2001 9:02 am
Has thanked: 0
Been thanked: 1 time

Post by BlackAura »

Just the one.
nymus
DC Developer
DC Developer
Posts: 968
Joined: Tue Feb 11, 2003 4:12 pm
Location: In a Dream
Has thanked: 5 times
Been thanked: 6 times

Post by nymus »

my suggestion is that you should use a boolean variable for "conf.fullscreen" instead of an int; most compilers can optimize booleans. saying 1 or 0 might be ambigous. use "true" and "false" instead. I agree that "comp.fullscreen=!comp.fullscreen" is the best choice.

If you are using c++, I would also seriously recommend wrapping such confusing or ambigous constructs in an inline function. Rand gave it a cool name (toggle) so If I were you, I would write a function

Code: Select all

inline void toggle(bool *const boolean) { *boolean = !(*boolean); }
you can then call the function like this

Code: Select all

toggle(&conf.fullscreen);
This is good because the code will not incur overhead, it will be as if you had written the code verbatim and it will be clearer beacuse instead of seeing the confusing "conf.fullscreen=!conf.fullscreen" everywhere in your code, you'll see "toggle(&conf.fullscreen) and you will be able to use a different algorithm by editing only one piece of code and you can use the toggle for other boolean variables too!
behold the mind
inspired by Dreamcast
Rand Linden
bleemcast! Creator
bleemcast! Creator
Posts: 882
Joined: Wed Oct 17, 2001 7:44 pm
Location: Los Angeles, CA
Has thanked: 0
Been thanked: 0
Contact:

Post by Rand Linden »

Using a bool is clearly preferable if you can only have logical choices -- and, accordingly, using the bang operator makes sense.

Also, you can pass the address as a reference, and then avoid the "&" entirely -- put it in the function prototype and forget about it.

Of course, you don't really need to pass it as a pointer anyway, but that's a whole seperate issue.

Rand.
User avatar
Quzar
Dream Coder
Dream Coder
Posts: 7499
Joined: Wed Jul 31, 2002 12:14 am
Location: Miami, FL
Has thanked: 4 times
Been thanked: 10 times
Contact:

Post by Quzar »

Rand Linden wrote:Using a bool is clearly preferable if you can only have logical choices -- and, accordingly, using the bang operator makes sense.
What's a bang operator?
"When you post fewer lines of text than your signature, consider not posting at all." - A Wise Man
mogorman
Insane DCEmu
Insane DCEmu
Posts: 127
Joined: Sat May 31, 2003 8:20 pm
Location: That place, down the street
Has thanked: 0
Been thanked: 0
Contact:

Post by mogorman »

! < bang
-------------------------------------------------
some say the glass is half full
some say the glass is half empty
I say thank god they gave me a glass
User avatar
Quzar
Dream Coder
Dream Coder
Posts: 7499
Joined: Wed Jul 31, 2002 12:14 am
Location: Miami, FL
Has thanked: 4 times
Been thanked: 10 times
Contact:

Post by Quzar »

ah, ok.
"When you post fewer lines of text than your signature, consider not posting at all." - A Wise Man
nymus
DC Developer
DC Developer
Posts: 968
Joined: Tue Feb 11, 2003 4:12 pm
Location: In a Dream
Has thanked: 5 times
Been thanked: 6 times

Post by nymus »

Rand Linden wrote: Also, you can pass the address as a reference, and then avoid the "&" entirely -- put it in the function prototype and forget about it.
Rand.
I passed it as a pointer to follow recommended convention of informing explicitly that the variable is being modified but in this case one could safely assume, I guess.
behold the mind
inspired by Dreamcast
Rand Linden
bleemcast! Creator
bleemcast! Creator
Posts: 882
Joined: Wed Oct 17, 2001 7:44 pm
Location: Los Angeles, CA
Has thanked: 0
Been thanked: 0
Contact:

Post by Rand Linden »

Here's a couple more tips to help you out:

Tip 1: The more frequently you ask questions that could (and SHOULD) have been answered by doing a search, the less frequently people will help you out.

Tip 2: When you post fewer lines of text than your signature, consider not posting at all.

Rand.
Rand Linden
bleemcast! Creator
bleemcast! Creator
Posts: 882
Joined: Wed Oct 17, 2001 7:44 pm
Location: Los Angeles, CA
Has thanked: 0
Been thanked: 0
Contact:

Post by Rand Linden »

nymus wrote:I passed it as a pointer to follow recommended convention of informing explicitly that the variable is being modified but in this case one could safely assume, I guess.
The const confuses things though, and hence the suggestion to avoid the pointer entirely.

Either way, do what works for you.

Rand.

edit: And again, that's why I suggested avoiding passing a pointer at all -- especially for a simple boolean toggle... although the inline tip is a good one!
User avatar
Quzar
Dream Coder
Dream Coder
Posts: 7499
Joined: Wed Jul 31, 2002 12:14 am
Location: Miami, FL
Has thanked: 4 times
Been thanked: 10 times
Contact:

Post by Quzar »

Rand Linden wrote:Here's a couple more tips to help you out:

Tip 1: The more frequently you ask questions that could (and SHOULD) have been answered by doing a search, the less frequently people will help you out.

Tip 2: When you post fewer lines of text than your signature, consider not posting at all.

Rand.
sorry, i tried doing a search but without knowing what the '?' operator was called it was hard to just look for "? C++". Then my second question about more ternary operators was asked because i did a search after finding out what it was called and some places said that it was "a ternary operator" and others said "the ternary operator" and i couldnt find a proper answer. The last question was frivilous i admit, but didnt cause any trouble.
"When you post fewer lines of text than your signature, consider not posting at all." - A Wise Man
Rand Linden
bleemcast! Creator
bleemcast! Creator
Posts: 882
Joined: Wed Oct 17, 2001 7:44 pm
Location: Los Angeles, CA
Has thanked: 0
Been thanked: 0
Contact:

Post by Rand Linden »

quzar, I raised the issue with repsect to the last post you made, not the earlier ones.

http://www.google.com -- Search for "bang operator" -- on the first page, without even going to a link, there are three (or more) references that show what the bang operator is.

Again, in the future, consider searching first if you're genuinely interested in programming.

Rand.
Post Reply