Faster way to create boolean IMP

BlitzMax Forums/BlitzMax Beginners Area/Faster way to create boolean IMP

Shortwind(Posted 2010) [#1]
Below is code I've quickly created to duplicate the function of the boolean operator IMP. The truth table is:

00=1
01=1
10=0
11=1

Function _Imp:Int(x:Int,y:Int)	'00=1, 01=1, 10=0, 11=1
	Local i:Int
	Local a:Int
	Local b:Int
	Local temp:Int=0
		
	For i=0 To 31
		a=readbit(x,i)
		b=readbit(y,i)
		If a=1 And b=0 Then
			temp=clearbit(temp,i)
		Else
			temp=setbit(temp,i)
		EndIf
	Next
	
	Return temp

End Function


I assume there is a easier way of doing this?
Thanks.


Czar Flavius(Posted 2010) [#2]
not (a and (not b)) or b

edit: after a google search, here's a better way!
(not a) or b


Shortwind(Posted 2010) [#3]
Cool! I knew I was seriously overthinking this. Thanks.

Ok, here are three more challenges for you. Order matters in these truth tables, I call them GOG, ZIR, YID

GOG
00=0
01=0
10=1
11=1

ZIR-(which I've figured out is ~x & y) [Read Not x And y]
00=0
01=1
10=0
11=0

YID-(which seems to be (~x & y) | y) [Read (Not x And y) Or y]
00=0
01=1
10=0
11=1

I have these working in code, but none are very efficient.

I don't know if these have real names or not, but these complete the proper 16 item truth table. Which of course includes AND, OR, XOR, IMP, NAND, NOR, NXOR, NIMP, and one I call TIE...


Floyd(Posted 2010) [#4]
GOG is the same as a, YID is the same as b.


Shortwind(Posted 2010) [#5]
Very astute observation there Floyd. :D


Czar Flavius(Posted 2010) [#6]
What are all these for??


Shortwind(Posted 2010) [#7]
What are all these for? Curiosity mostly.

The most commonly used, AND/OR, are everywhere. Anyone who has been alive more than a couple of years fully understands their meaning.

In computer circuitry the very processors that power our machines are controlled by these basic ideas. (With NAND gates being quite popular.)

XOR is better known in the RAID industry. The NXOR is called EQV in some texts and programming lanugages.

As for the others, I suppose my interpretation of GOG, and YID appear on the surface to be quite useless. IMP is sometimes included in some programming languages, but isn't used all that much. As for my ZIR? I don't know yet, that's what peeked my curiosity.

From a gaming standpoint lets take a look at IMP. Here is a quick demonstration of how this would work:

A character comes to a door,
If the door is unlocked, and he doesn't have a key, open the door.
If the door is unlocked, and he has the key, open the door.
If the door is locked, and he doesn't have the key, can't open the door.
If the door is locked, and he has the key, then open the door.

So in a sense, four conditional statements can be replaced by:
If (DoorLock IMP HasKey) then OpenDoor

(Ok, maybe not the best example, but I hope you get my point.)

Also, since all of these are basically just combinations of NOT, AND, and OR, I suppose the need for any of the others could be viewed as pointless.