Not, is a operator, but not is a function

Just starting out? Need help? Post your questions and find answers here.
SP
User
User
Posts: 33
Joined: Wed Aug 16, 2006 2:32 pm
Location: BCN-SPAIN

Not, is a operator, but not is a function

Post 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
DarkDragon
Addict
Addict
Posts: 2345
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

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

Post 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
bye,
Daniel
u9
User
User
Posts: 55
Joined: Tue May 09, 2006 8:43 pm
Location: Faroe Islands
Contact:

Post by u9 »

I guess you have to use parantheses like this:

Code: Select all

a = (Not a)
The most likely way for the world to be destroyed, most experts agree, is by accident. That's where we come in; we're computer professionals. We cause accidents. (Nathaniel Borenstein)
http://www.wirednerd.com
SP
User
User
Posts: 33
Joined: Wed Aug 16, 2006 2:32 pm
Location: BCN-SPAIN

Post by SP »

Hi Daniel,

Easy and very clear!
Thanks

Greetings
Santi
DarkDragon
Addict
Addict
Posts: 2345
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post 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.
bye,
Daniel
SP
User
User
Posts: 33
Joined: Wed Aug 16, 2006 2:32 pm
Location: BCN-SPAIN

Post by SP »

OK Daniel and u9. This works!

Greetings!
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

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

Post by PB »

> How can I simplify this code?

Code: Select all

a=1-a
:)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

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

Post 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
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

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

Post 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
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

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

Post 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.
SP
User
User
Posts: 33
Joined: Wed Aug 16, 2006 2:32 pm
Location: BCN-SPAIN

Post 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
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

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

Post 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
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

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

Post 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. :)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Froggerprogger
Enthusiast
Enthusiast
Posts: 423
Joined: Fri Apr 25, 2003 5:22 pm
Contact:

Post 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
...
%1>>1+1*1/1-1!1|1&1<<$1=1
Post Reply