[Implemented] Logic functions

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

[Implemented] Logic functions

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.

You might think this is me being thick, and not really a feature request :)

Well, I request information about this stuff to be added to the manual if PB can already do it :)

My problem is logic functions. I can't seem to get them to work.

a.w=1 And 3 : PrintN(Str(a)) ; Gives answer of 1 = correct
a.w=1 Or 3 : PrintN(Str(a)) ; Gives answer of 0 = wrong
Don't even get me started on exclusive or - since there seems to be no function for that.

Cheers.


--
It's not minimalist - I'm increasing efficiency by reducing input effort.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.
You might think this is me being thick, and not really a feature request :)

Well, I request information about this stuff to be added to the manual if PB can already do it :)

My problem is logic functions. I can't seem to get them to work.

a.w=1 And 3 : PrintN(Str(a)) ; Gives answer of 1 = correct
a.w=1 Or 3 : PrintN(Str(a)) ; Gives answer of 0 = wrong
Don't even get me started on exclusive or - since there seems to be no function for that.
Hi,
why not try:
a.w = 1 & 3 ; instead of "1 and 3"
a.w = 1 | 3 ; instead of "1 or 3"

I don't know if this is in the documentation and actually is explicitly explained, but in many examples that go with the manual you can see the use of these operators every so often...
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.
why not try:
a.w = 1 & 3 ; instead of "1 and 3"
a.w = 1 | 3 ; instead of "1 or 3"

I don't know if this is in the documentation and actually is explicitly explained, but in many examples that go with the manual you can see the use of these operators every so often...
Doh, I completely forgot about the alternatives for those. I have even
used | in the past in some PB test programs myself :(

But the question of exclusive or still remains (OK, I know you can
make it out of not, and and or, but that's fairly inconvenient). ^
Doesn't work, I've tried that and gives a compiler error.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Franco.
^ Doesn't work, I've tried that and gives a compiler error.
hi Tinman, you get the power with:

Pow(Number.f, Power.f)

for now. Don't know if ^ will be in the core language one fine day...


Have a nice day...
Franco

Sometimes you have to go a lonely way to accomplish genius things.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.
Doh, I completely forgot about the alternatives for those. I have even
used | in the past in some PB test programs myself :(

But the question of exclusive or still remains (OK, I know you can
make it out of not, and and or, but that's fairly inconvenient). ^
Doesn't work, I've tried that and gives a compiler error.
Exclusive or, or XOR for short, is unfortunately not as simple in PB as 'AND' and 'OR' operations, not yet at least. Whenever i need this i have to tinker a bit with assembly code, on a x86 processor the code could look something like this:

Code: Select all

a.l=$ff
b.l=$7f
mov eax,a
xor b,eax
messagerequester("","Result="+str(b),0)
end
Unfortunately i'm not very good at this so when you mix type i.e. byte,word or long, i usually get in trouble.
Hope this post might help some though!
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by plouf.

all this recall me a c fuction for using xor
c hasn't xor operator either
(i never use logic fuctions up to now )

Procedure.b xor(a.l,b.l)
If (a|b) & (a&b)=0
ProcedureReturn 1 ; or #TRUE
Else
ProcedureReturn 0 ; or #FALSE
EndIf
EndProcedure


Christos
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.
all this recall me a c fuction for using xor
c hasn't xor operator either
{/quote]

Yes, it uses the ^ operator.
Procedure.b xor(a.l,b.l)
If (a|b) & (a&b)=0
ProcedureReturn 1 ; or #TRUE
Else
ProcedureReturn 0 ; or #FALSE
EndIf
EndProcedure
Thanks, but I wanted to avoid that. And the way I was going to use it was as a bitwise operator, so I wouldn`t simply return 0 or 1, but the result of the logic operation (so your code would not work).

Pupil, thanks for the suggestion, but I'm not going to stick in x86 assembly language when one of the reasons I use PB is for portability. And an XOR is as simply as AND and OR`s (OK, I forgot to mention NOT :)

z = /a.b + a./b

Thanks for all your suggestions though.


--
It's not minimalist - I'm increasing efficiency by reducing input effort.
Post Reply