Logic Primitives

It has been said that one can implement any logical statement using only NOR logic. These are the primitives for AND, OR, NAND, NOR, XOR, XNOR, and NOT. They are phrased as comparisons, but IF EXIST tests work the same way. With these, you should be able to build your own batch file super computer. Actual construction is left as an exercise for the reader.
The logic convention is that TRUE = 1 and FALSE = 0.


 :and
 set result=0
 if foo == bar if baz == qux set result=1

 :or
 set result=0
 if foo == bar set result=1
 if baz == qux set result=1

 :nor
 set result=0
 if not foo == bar set result=1
 if not baz == qux set result=1

 :nand
 set result=0
 if not foo == bar if not baz == qux set result=1

 :xor
 set result=0
 if foo == bar if not baz == qux set result=1
 if not foo == bar if baz == qux set result=1

 :xnor
 set result=0
 if not foo == bar if not baz == qux set result=1
 if foo == bar if baz == qux set result=1

 :negation
 if %result% == 0 set result=x
 if %result% == 1 set result=0
 if %result% == x set result=1

These can be combined through intermediate variables as needed to extend the logic to more complex functions or to deal with large numbers of inputs (too many to put on one line). For examples, a test for the existence of eight files might be written this way


 :8and
 set result=0
 set foo14=0
 set foo58=0
 if exist foo1 if exist foo2 if exist foo3 if exist foo4 set foo14=1
 if exist foo5 if exist foo6 if exist foo7 if exist foo8 set foo58=1
 if %foo14% == 1 if %foo58% == 1 set result=1
a test for any one of eight files might be this

 :8or
 set result=0
 if exist foo1 set result=1 
 if exist foo2 set result=1 
 if exist foo3 set result=1 
 if exist foo4 set result=1 
 if exist foo5 set result=1 
 if exist foo6 set result=1 
 if exist foo7 set result=1 
 if exist foo8 set result=1 
and a test for the existence of at least two files from a set of four AND simultaneously (two environment variables equal to each other OR a third not being null) could be written like this - the logic equation is
((A*B)+(A*C)+(A*D)+(B*C)+(B*D)+(C*D))*((E1=E2)+E3) = 1
 
 :complex
 set result=0
 set x1=0
 set x2=0

 if exist A if exist B set x1=1
 if exist A if exist C set x1=1
 if exist A if exist D set x1=1

 if exist B if exist C set x1=1
 if exist B if exist D set x1=1

 if exist C is exist D set x1=1

 if %E1% == %E2% set x2=1

 if not "%E3%" == "" set x2=1

 if %x1% == 1 if %x2% == 1 set result=1
or like this - the logic equation is
(((A*(B+C+D))+(B*(C+D))+(C*D))*((E1=E2)+E3) = 1

 :complex
 set result=0
 set x1=0
 set x2=0

 if exist B set x1=1
 if exist C set x1=1
 if exist D set x1=1

 if exist A if %x1% == 1 set x2=1
 
 set x1=0
 if exist C set x1=1
 if exist D set x1=1

 if exist B if %x1% == 1 set x2=1

 if exist C if exist D set x2=1

 set x1=0
 if %E1% == %E2% set x1=1
 if not "%E3%" == "" set x1=1

 if %x1% == 1 if %x2% == 1 set result=1
(the blanks lines are for clarity only, and would be omitted in a real program). In the above logic equations, certain liberties have been taken to restrict the character set to pure ASCII: '*' means AND, '+' means OR, "=" signifies identify, and if I had used negation, it would have been noted with '!'. The character density of the equations is required by the limited line length available.

  ** Copyright 1995, Ted Davis - all rights reserved ** 

Input and feedback from readers are welcome. NOTE: the subject of the message must contain the word "batch" for the message to get past the spam filter.

Back to the Table of Contents page

Back to my personal links page - back to my home page