WD: Simulating XOR Logic with Field Codes or WordBasic

ID: Q80510

The information in this article applies to:

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 "XOR is True"
      Else
         Print "XOR is False"
      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 ="T" "T" "F"} = {IF y ="T" "T" "F"} "F" "T"}

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$="T" THEN Test1$="T" ELSE Test1$="F"
   IF Y$="T" THEN Test2$="T" ELSE Test2$="F"
   IF Test1$=Test2$ THEN
      Results$="F" 
   ELSE
      Results$="T"
   END IF

For more information on the If...Endif statement, search for "WordBasic Programming Language" and "If...ElseIf...Else...End If" using the Help menu.

REFERENCES

"Microsoft Word for Windows User's Guide," version 2.0, pages 747-762, 774-776

"Microsoft Excel Function Reference," version 3.0, page 155

"GW-Basic Interpreter User's Guide," page 60

Additional query words:

Keywords          : kbmacro wordnt kbmacroexample kbfield ntword macword word6 word7 word95 
Version           : WINDOWS:1.0,1.1,1.1a,2.0,2.0a,2.0a- CD,2.0b,2.0c,6.0,6.0a,6.0c,7.0,7.0a; MACINTOSH:6.0,6.0.1,6.0.1a
Platform          : MACINTOSH WINDOWS
Issue type        : kbhowto

Last Reviewed: February 4, 1998