Simple toggle trick

Share your advanced PureBasic knowledge/code with the community.
Randy Walker
Addict
Addict
Posts: 1034
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Simple toggle trick

Post 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
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
User avatar
Bisonte
Addict
Addict
Posts: 1313
Joined: Tue Oct 09, 2007 2:15 am

Re: Simple toggle trick

Post 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
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
AZJIO
Addict
Addict
Posts: 2165
Joined: Sun May 14, 2017 1:48 am

Re: Simple toggle trick

Post 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
BarryG
Addict
Addict
Posts: 4162
Joined: Thu Apr 18, 2019 8:17 am

Re: Simple toggle trick

Post by BarryG »

I've always used "t=1-t" because it makes it clear to anyone what it does.
User avatar
minimy
Enthusiast
Enthusiast
Posts: 607
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: Simple toggle trick

Post by minimy »

l= 1
l!1
Debug l
l!1
Debug l
This work too
If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
Piero
Addict
Addict
Posts: 894
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Simple toggle trick

Post 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:
Last edited by Piero on Thu Aug 21, 2025 10:13 pm, edited 1 time in total.
Randy Walker
Addict
Addict
Posts: 1034
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Simple toggle trick

Post by Randy Walker »

Indeed.... more good tricks. Thanks everyone for your contributions!!
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Randy Walker
Addict
Addict
Posts: 1034
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Simple toggle trick

Post 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
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
User avatar
Bisonte
Addict
Addict
Posts: 1313
Joined: Tue Oct 09, 2007 2:15 am

Re: Simple toggle trick

Post 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 ... ;)
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
Randy Walker
Addict
Addict
Posts: 1034
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Simple toggle trick

Post 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.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
SMaag
Enthusiast
Enthusiast
Posts: 318
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: Simple toggle trick

Post 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
Randy Walker
Addict
Addict
Posts: 1034
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Simple toggle trick

Post 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. :)
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
User avatar
minimy
Enthusiast
Enthusiast
Posts: 607
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: Simple toggle trick

Post 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
If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
mk-soft
Always Here
Always Here
Posts: 6235
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Simple toggle trick

Post by mk-soft »

Is not better because ABS first converts everything into double and then back again
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 6235
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Simple toggle trick

Post 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

My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply