Page 1 of 2

Simple toggle trick

Posted: Thu Aug 21, 2025 4:11 am
by Randy Walker
Long time ago I cane across the need to toggle a variable off and on. Until recently I forgot about this trick. This little snippet demonstrates how the tilde (not) symbol can be used to toggle a variable on each pass.
Run with or without Debugger:

Code: Select all

Repeat
  Select MessageRequester("See toggle Work?", Str(t)+" <<Yes to toggle this value.", #MB_YESNO)
    Case #IDYES
      t = ~ t
    Case #IDNO
      Break
  EndSelect
  Debug t
ForEver
End

Re: Simple toggle trick

Posted: Thu Aug 21, 2025 7:35 am
by Bisonte
If this is supposed to be a tip, and it is found by a beginner...
they will be confused at first.

What are these constants?

Please use PureBasic constants and not API constants (which, to my knowledge, only exist in Windows),
even if they are quicker to write... Not everyone use your environment and write in your style.

Thank you.

in this case

Code: Select all

#PB_MessageRequester_YesNo
#PB_MessageRequester_Yes
#PB_MessageRequester_No

Re: Simple toggle trick

Posted: Thu Aug 21, 2025 7:36 am
by AZJIO

Code: Select all

t = Bool(Not t)
If you read the flag from the ini file and someone writes 15 there, your version will stop working

Re: Simple toggle trick

Posted: Thu Aug 21, 2025 8:56 am
by BarryG
I've always used "t=1-t" because it makes it clear to anyone what it does.

Re: Simple toggle trick

Posted: Thu Aug 21, 2025 3:12 pm
by minimy
l= 1
l!1
Debug l
l!1
Debug l
This work too

Re: Simple toggle trick

Posted: Thu Aug 21, 2025 7:01 pm
by Piero

Code: Select all

a=6
a!4
Debug a
a!4
Debug a
a!4
Debug a

Code: Select all

a=~4
Debug 4&a|2
a=~a
Debug 4&a|2
a=~a
Debug 4&a|2
viewtopic.php?p=635000#p635000

:wink:

Re: Simple toggle trick

Posted: Thu Aug 21, 2025 7:46 pm
by Randy Walker
Indeed.... more good tricks. Thanks everyone for your contributions!!

Re: Simple toggle trick

Posted: Thu Aug 21, 2025 7:56 pm
by Randy Walker
Bisonte wrote: Thu Aug 21, 2025 7:35 am If this is supposed to be a tip, and it is found by a beginner...
they will be confused at first.

What are these constants?

Please use PureBasic constants and not API constants
Thank you.
Wow!! Didn't think it would offend anyone. Have it your way...

Code: Select all

Repeat
  Select MessageRequester("See toggle Work?", Str(t)+" <<Yes to toggle this value.", #PB_MessageRequester_YesNo)
    Case #PB_MessageRequester_Yes
      t = ~ t
    Case #PB_MessageRequester_No
      Break
  EndSelect
  Debug t
ForEver
End

Re: Simple toggle trick

Posted: Fri Aug 22, 2025 7:18 am
by Bisonte
Randy Walker wrote: Thu Aug 21, 2025 7:56 pm
Bisonte wrote: Thu Aug 21, 2025 7:35 am If this is supposed to be a tip, and it is found by a beginner...
they will be confused at first.

What are these constants?

Please use PureBasic constants and not API constants
Thank you.
Wow!! Didn't think it would offend anyone. Have it your way...
...
Sorry its no offend... But your codes often have some mystical things (for beginners), their are
not in the PB Help ... ;)

Re: Simple toggle trick

Posted: Fri Aug 22, 2025 3:56 pm
by Randy Walker
Bisonte wrote: Fri Aug 22, 2025 7:18 am Wow!! Didn't think it would offend anyone. Have it your way...
...
Sorry its no offend... But your codes often have some mystical things (for beginners), their are
not in the PB Help ... ;)
[/quote]
Yes, my posts are intended mostly for beginners. I know I'm not going to teach any seasoned users here anything.

Re: Simple toggle trick

Posted: Fri Aug 22, 2025 5:12 pm
by SMaag
Hi Randy, your code is a good example for don't do that!

Never try to toggle ON/OFF with the binary not! Always use Bool Not.
This is a big differernce.
The binary Not toggles all bits.
The bool Not toggles between #False and #True.

I modified your code to show the Problem.
If I set first t=#True (= 1) and use binary Not, It toggles between 1 and -2 what both is #True.
So you do not get ON/OFF!

Code: Select all

t= #True
Repeat
  Select MessageRequester("See toggle Work?", Str(t)+" <<Yes to toggle this value.", #MB_YESNO)
    Case #IDYES
      t = ~ t
    Case #IDNO
      Break
  EndSelect
  Debug Bool(t)
ForEver
End
if you want to toggle a Bool just do a 0 check!

Code: Select all

Macro ToggleBool(_val_)
 Bool(_val_=0)
EndMacro

t = 1234
Debug Str(t) + " : " + Bool(t)
Debug ""
For I = 1 To 10
  t = ToggleBool(t)
  Debug t
Next

Re: Simple toggle trick

Posted: Fri Aug 22, 2025 5:45 pm
by Randy Walker
SMaag wrote: Fri Aug 22, 2025 5:12 pm Hi Randy, your code is a good example for don't do that!
Great!! Thanks for pointing that out. Makes my Original post even more worth while. :)

Re: Simple toggle trick

Posted: Fri Aug 22, 2025 6:02 pm
by minimy
One way more :P

Code: Select all

a= 1
Debug a
a= Abs(a-1)
Debug a
a= Abs(a-1)
Debug a

Re: Simple toggle trick

Posted: Fri Aug 22, 2025 6:06 pm
by mk-soft
Is not better because ABS first converts everything into double and then back again

Re: Simple toggle trick

Posted: Fri Aug 22, 2025 6:21 pm
by mk-soft
Short way for ASM and C backend

Code: Select all

Macro Toggle(x)
  x=~x&1
EndMacro

a=#True
Toggle(a)
Debug a
Toggle(a)
Debug a
Toggle(a)
Debug a