Author Topic: instruction boolean QB64 true o false  (Read 4902 times)

0 Members and 1 Guest are viewing this topic.

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: instruction boolean QB64 true o false
« Reply #15 on: October 09, 2020, 08:38:10 pm »
Here is a fun tip: if you want to toggle a variable between two values 'a' and 'b' then xor it by (a xor b),

z = 7
z = z xor (7 xor 19) '=19
z = z xor (7 xor 19) '=7
z = z xor (7 xor 19) '=19
z = z xor (7 xor 19) '=7
'... and so on

Code: [Select]
z = 7 'start value
a = 0

for i=1 to 10
? chr$(asc("T")*-(a<>0)+asc("F")*-(a=0))+chr$(asc("R")*-(a<>0)+asc("A")*-(a=0))+chr$(asc("U")*-(a<>0)+asc("L")*-(a=0))+chr$(asc("E")*-(a<>0)+asc("S")*-(a=0))+chr$(asc("E")*-(a=0)), a, z


z = z xor (7 xor 19)
a = a xor (0 xor not 0)
next

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: instruction boolean QB64 true o false
« Reply #16 on: October 09, 2020, 10:55:18 pm »
var XOR 1 is faster, in theory, than var = 1 - var,
because only one register is involved. 

Vince's toggle method is neat, and new to me.  It
even works for flipping between 0 and 1.  But it
does incur 3 register loads and 2 register operations.

Speed doesn't much matter for normal programs, but
it sure does for things like chess, lest the user die of
old age before a move is made.
It works better if you plug it in.

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: instruction boolean QB64 true o false
« Reply #17 on: October 10, 2020, 07:58:04 pm »
Vince's toggle method is neat, and new to me.  It
even works for flipping between 0 and 1.  But it
does incur 3 register loads and 2 register operations.

Vince does cool stuff, am I right that the "TRUE" / "FALSE" is a example of "branchless" programming?