Microsoft KB Archive/80510

{|
 * width="100%"|

WD: Simulating XOR Logic with Field Codes or WordBasic

 * }

Q80510

-

The information in this article applies to:


 * Microsoft Word for Windows, versions 1.0, 1.1, 1.1a, 2.0, 2.0a, 2.0a-CD, 2.0b, 2.0c, 6.0, 6.0a, 6.0c
 * Microsoft Word for Windows NT, version 6.0
 * Microsoft Word for Windows, versions 7.0, 7.0a
 * Microsoft Word for the Macintosh, versions 6.0, 6.0.1

-

SUMMARY
This article describes how to simulate the XOR logic in Microsoft Word by using either field codes or the WordBasic macro language.

NOTE: Beginning with Word 97, Visual Basic for Applications includes the XOR logical operator.

MORE INFORMATION
To reproduce the XOR operation in Word for Windows using a WordBasic function, use the following formula

  XOR=(Abs(test1 + test2 +... + testn) Mod 2) * - 1 for example:

  Sub MAIN a =(2 = 2) b =(2 = 3) c =(2 = 3) If XOR(a, b, c) Then Print &quot;XOR is True&quot; Else Print &quot;XOR is False&quot; End If  End Sub

Function XOR(a, b, c)     XOR =  (Abs(a + b + c) Mod  2) * - 1 End Function The following is an explanation of how the function works:


 * 1) The formula applies the ABS function to the sum of the variables that return TRUE. Note: In Word for Windows, TRUE has the numerical value of negative one (-1), FALSE equals zero (0).
 * 2) The MOD statement divides this sum by two (2) and returns the remainder, which is one (1) if an odd number of the tests are TRUE, and zero (0) if an even number of the tests are TRUE.
 * 3) The remainder is multiplied by negative one. FALSE remains zero, but TRUE becomes negative one (-1) to conform to WordBasic notation.

You can create this type of logical structure in Word for Windows by using field codes. In the following example, T=TRUE and F=FALSE. Note: To insert the braces, press CTRL+F9.

  {IF {IF x =&quot;T&quot; &quot;T&quot; &quot;F&quot;} = {IF y =&quot;T&quot; &quot;T&quot; &quot;F&quot;} &quot;F&quot; &quot;T&quot;} In the above example, the field on either side of the equal sign (=) checks to see if the comparison is TRUE. If it is, the results are TRUE; if not, the result is FALSE. The comparison of the two IF statements yields TRUE if they are the same and FALSE if they are not. The following table illustrates the values and the results:

  Statement 1       Statement 2      Results ---      ---      ---

T                 T              F       T                  F              T       F                  T              T       F                  F              F You can also create this kind of logical structure in a Word for Windows macro. In the following example, T=TRUE and F=FALSE. In WordBasic, the IF statement is combined with the THEN and ELSE statements. An example would be as follows:

  IF X$=&quot;T&quot; THEN Test1$=&quot;T&quot; ELSE Test1$=&quot;F&quot; IF Y$=&quot;T&quot; THEN Test2$=&quot;T&quot; ELSE Test2$=&quot;F&quot; IF Test1$=Test2$ THEN Results$=&quot;F&quot; ELSE Results$=&quot;T&quot; END IF For more information on the If...Endif statement, search for &quot;WordBasic Programming Language&quot; and &quot;If...ElseIf...Else...End If&quot; using the Help menu.