Page 1 of 2

Not, is a operator, but not is a function

Posted: Sat Aug 26, 2006 1:53 pm
by SP
How can I simplify this code?
If a
a = 0
Else
a = 1
EndIf

In other languages is very easy (a = NOT a), but in PB not works.

Thanks in advance

Re: Not, is a operator, but not is a function

Posted: Sat Aug 26, 2006 2:07 pm
by DarkDragon
SP wrote:How can I simplify this code?
If a
a = 0
Else
a = 1
EndIf

In other languages is very easy (a = NOT a), but in PB not works.

Thanks in advance
Works here:

Code: Select all

a.l = 0
Debug (Not a) Or #False

a.l = 1
Debug (Not a) Or #False

Posted: Sat Aug 26, 2006 2:19 pm
by u9
I guess you have to use parantheses like this:

Code: Select all

a = (Not a)

Posted: Sat Aug 26, 2006 2:20 pm
by SP
Hi Daniel,

Easy and very clear!
Thanks

Greetings
Santi

Posted: Sat Aug 26, 2006 2:23 pm
by DarkDragon
And this one works, too:

Code: Select all

a.l = 0
Debug (Not a)

a.l = 1
Debug (Not a)
But first one is more secure afaik, because purebasic doesn't know boolean expressions, it's more like a integer expression.

Posted: Sat Aug 26, 2006 2:30 pm
by SP
OK Daniel and u9. This works!

Greetings!

Posted: Sat Aug 26, 2006 2:38 pm
by Trond
This works here, but it is definetely not intentional:

Code: Select all

a.l = 0
a = _ Not a
Debug a

a.l = 1
a = _ Not a
Debug a

Re: Not, is a operator, but not is a function

Posted: Sat Aug 26, 2006 3:30 pm
by PB
> How can I simplify this code?

Code: Select all

a=1-a
:)

Re: Not, is a operator, but not is a function

Posted: Sat Aug 26, 2006 4:03 pm
by Trond
PB wrote:> How can I simplify this code?

Code: Select all

a=1-a
:)
Too much:

Code: Select all

; Original code
a = 5
If a 
  a = 0 
Else 
  a = 1 
EndIf
Debug a

; Your code
a = 5
a=1-a
Debug a

Re: Not, is a operator, but not is a function

Posted: Sat Aug 26, 2006 4:09 pm
by PB
@Trond: The original code by SP does not specify that a=5, and only shows
that he wants to toggle between 0 and 1, which is exactly what a=1-a does.

@SP: In PureBasic you'd do it like this:

Code: Select all

a=~a
Because PureBasic uses ~ instead of Not in your original post. Observe:

Code: Select all

Repeat
  a=~a
  Debug a
  c+1
Until c=4

Re: Not, is a operator, but not is a function

Posted: Sat Aug 26, 2006 5:12 pm
by Trond
PB wrote:@Trond: The original code by SP does not specify that a=5, and only shows
that he wants to toggle between 0 and 1, which is exactly what a=1-a does.
No, since the original post does not specify what a is you cannot assume that it is either 0 or 1. It could be -4562.

The code does not toggle between 0 and 1, it toggles between 0 and not 0. It says "if a", not "if a = 1".

I might as well post this:

Code: Select all

a=1
It's much simpler than everything else so far. Sure, it doesn't do what he asked for, but so what? Pick a random value for a and it gives the correct result almost every time.

Posted: Sat Aug 26, 2006 6:46 pm
by SP
Hi Trond and PB,

Thanks for all your posts.
Really you are PB cracks!

All solutions are valid for me, because in this case, the initial value is always '0'.

Greetings

Re: Not, is a operator, but not is a function

Posted: Sat Aug 26, 2006 8:28 pm
by Dr. Dri
To toggle between 0 and 1 a!1 is easier but in his code it seems that "true" is not "1" but "non zero"

Dri

Re: Not, is a operator, but not is a function

Posted: Sun Aug 27, 2006 2:42 am
by PB
> the original post does not specify what a is you cannot assume

True, but I was taking an educated guess based on the fact that no specific
info was provided that enabled us to be 100% sure of his intentions, especially
when you consider he gave an example of "a = Not a", so it's obvious that he
wanted a toggle of some sort, and his code referenced both 0 and 1, so I think
it was a very good assumption to make. :)

Posted: Sat Sep 09, 2006 9:23 am
by Froggerprogger
just 2 cents:
you might toggle a between any two values x and y by using:

Code: Select all

x = 1234
y = -17
toggle = y ! x

a = x
a ! toggle
a ! toggle
...
So e.g. between 42 and 24

Code: Select all

a = 24
toggle = 50 ; 42 ! 24

a ! toggle
a ! toggle
...