Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / Switch a boolean variable using formula (no if statement)

Author
Message
Neil Young
21
Years of Service
User Offline
Joined: 6th Jan 2004
Location:
Posted: 30th Aug 2005 01:58 Edited at: 30th Aug 2005 02:00
I wanted to switch a boolean variable back and forth between 0 and 1 during a specific function without using hard-coded if statements (to determine it's present value first and then switch its value accordingly). Pure math can't do this alone, but when I went to sit on the can the wrapvalue command occured to me.



This was a tough puzzle at the time but I figured a good solution (maybe there is better if someone can contribute). Hope this makes your code more friendly!
Lost in Thought
21
Years of Service
User Offline
Joined: 4th Feb 2004
Location: U.S.A. : Douglas, Georgia
Posted: 30th Aug 2005 11:06 Edited at: 30th Aug 2005 11:07
Cool but using if is way faster.



Appx 6.5 times faster here.



Math89
21
Years of Service
User Offline
Joined: 23rd Jan 2004
Location: UK
Posted: 30th Aug 2005 11:50
You can use NOT : value = not value.
Lost in Thought
21
Years of Service
User Offline
Joined: 4th Feb 2004
Location: U.S.A. : Douglas, Georgia
Posted: 30th Aug 2005 13:50
Yes you can use the not operator, but remember it only tests bit 0 so it may be 1 cycle off if the beginning value is other than 1 or 0(like the wrapvalue method) when starting. Thats why I used > 0 as the test to keep it boolean from the beginning. Also the NOT operator is slightly the fastest method, I don't see how I forgot it

TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 31st Aug 2005 07:42
Quote: "Also the NOT operator is slightly the fastest method, I don't see how I forgot it"


And lets not forget that DBC users haven't got the NOT operator...

TDK_Man

Sven B
20
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 31st Aug 2005 12:47
Improvisation:





Immunity and Annihalation makes Immunihalation...
Jukuma
21
Years of Service
User Offline
Joined: 18th Nov 2003
Location: Germany
Posted: 31st Aug 2005 18:18
this works for all i think

value = abs(value - 1)
Jukuma
21
Years of Service
User Offline
Joined: 18th Nov 2003
Location: Germany
Posted: 1st Sep 2005 02:25
do
print abs(null - 1)
wait 1000
loop

bye
empty
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: 3 boats down from the candy
Posted: 1st Sep 2005 03:06
or even shorter

value = 1 - value


Play Nice! Play Basic! Version 1.088
Xolatron old
21
Years of Service
User Offline
Joined: 25th Jan 2004
Location: The Star Forge Language: DBpro
Posted: 1st Sep 2005 03:08
Or use this great code without if's; I really think it's probably fastest (basically it's a better NOT that will work in DBC)


-Xol


DBpro IonRay IDE: Demo 0.1.0.0 available!
BatVink
Moderator
22
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 1st Sep 2005 16:44
Nice one, empty, you win!

Xolatron, your value will always be 0!

Phaelax
DBPro Master
22
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 1st Sep 2005 16:57
I always like to make constants in my program:

#constant TRUE = 1
#constant FALSE = 0

Your signature has been erased by a mod because it's larger than 600x120...
Xolatron old
21
Years of Service
User Offline
Joined: 25th Jan 2004
Location: The Star Forge Language: DBpro
Posted: 2nd Sep 2005 05:00
BatVink, really¿
(1=0) returns 0
(0=0) returns 1
bool = 1
(bool=0) returns 0
bool = 0
(bool=0) returns 1

-Xol


DBpro IonRay IDE: Demo 0.1.0.0 available!
Neil Young
21
Years of Service
User Offline
Joined: 6th Jan 2004
Location:
Posted: 4th Sep 2005 05:14
Wow those are great. Thanks! This is why I love programming.
Xolatron old
21
Years of Service
User Offline
Joined: 25th Jan 2004
Location: The Star Forge Language: DBpro
Posted: 4th Sep 2005 06:27
Yes, me too . There's always a better way, or, for Star Wars fans out there, "There's always a bigger fish."

-Xol


DBpro IonRay IDE: Demo 0.1.0.0 available!
Jukuma
21
Years of Service
User Offline
Joined: 18th Nov 2003
Location: Germany
Posted: 4th Sep 2005 06:56
sry, bad code over there from me.
i think the fastes one, if the copilation
asm "lee say dbp makes pure asm code "

null as byte
null = null or %10101010

do

print null and %1

null = null >> 1
wait 1000

loop
Jukuma
21
Years of Service
User Offline
Joined: 18th Nov 2003
Location: Germany
Posted: 4th Sep 2005 07:12 Edited at: 4th Sep 2005 07:26
noooooo, ">>" shifting ?? wy don't ">> not rotate the bit's?

before a bad code und now - a sorry again!
coolgames
19
Years of Service
User Offline
Joined: 26th Sep 2005
Location: Oregon, USA
Posted: 26th Sep 2005 21:45
This will run very fast. It uses a simple equation:

That will switch x between 1 and 0
coolgames
19
Years of Service
User Offline
Joined: 26th Sep 2005
Location: Oregon, USA
Posted: 26th Sep 2005 22:02
You actually can use pure math to switch between 1 and 0

Login to post a reply

Server time is: 2025-05-15 22:37:59
Your offset time is: 2025-05-15 22:37:59