Android Code Snippet Switching between 0 and 1

=======================================

EDIT:
I just got pwned by @GKCS, a = 1 - a.
I guess I like to over complicate things...

==========================================

Everyone knows how easy it is to switch a Boolean "on and off":
B4X:
myBool = (myBool == False) 'Uglier and faster than Not(myBool)


...but how about switching a Short or Int between 0 and 1?
B4X:
Dim a = 0 as Short
'Some loop
    If a = 0 Then a = 1 Else a = 0
'End of loop

Not bad but can we do it in a single statement?
B4X:
Dim a = 0 As Short
'Some loop
    a = Bit.Xor(0x0001, a) '0x00000001 for Int
'End of loop
Pretty awesome, huh? ;) :D

Capture.png
 
Last edited:

sorex

Expert
Licensed User
Longtime User
depending on the situation of having a general counter like in your loop example one could use

B4X:
for x=0 to 5
log(x mod 2)
next

or

for x=0 to 5
log(bit.and(x,1)
next
 
Top