Reg Exp
Web Design
Notes Client
Exclusive Or With Strings
Let's say you need to keep track of things that happen on certain days of the week. This is done through a 7 character string where each character is a "0" (didn't happen) or a "1" (did happen). The first character relates to Sunday, the second to Monday, and so on. A string of "0010010" would mean this 'thing' happend on Tuesday and Friday.

Here's an Exclusive Or function that will flip bit's to "1". Exclusive Or says that if either bit is a 1 (in either string) then it's a 1 in the destination. So, if you take the above string above (Tuesday and Friday set) and want to set Saturday, you can Exclusive Or the first string with "0000001" and get the final string of "0010011" which indicates the 'thing' happened on Tuesday, Friday, and Saturday.

Function ExclusiveOr(inpValue1 As String, inpValue2 As String) As String
   On Error Goto BubbleError
   ' Two strings of 0's and 1's are combined in an exclusive or fashion - if either "bit" has
   ' a 1, the value is 1 and if both are 0's the value is 0.
   Dim value1 As String
   Dim value2 As String
   Dim retVal As String
   Dim comp As String
   Dim i As Integer

   ' If one string is shorter than the other, pad THE FRONT of the shorter string with 0's
   value1 = inpValue1
   value2 = inpValue2
   If Len(value1) < Len(value2) Then value1 = String$(Len(value2)-Len(value1), "0") & value1
   If Len(value2) < Len(value1) Then value2 = String$(Len(value1)-Len(value2), "0") & value2

   ' If either string has something besides 0's and 1's, return AN EMPTY STRING
   comp = ""
   For i = 1 To Len(value1)
      comp = comp & "[01]" ' This will the the pattern - every character is 0 or 1 and there's "x" of them
   If (Not value1 Like comp) Or (Not value2 Like comp) Then
      ExclusiveOr = ""
      Exit Function ' ============================================================
   End If

   ' Go through every character and compare the same position on the two strings. If both are
   ' zero, then put 0 in the return string. Otherwise one of the two strings (or both) has a
   ' one in that position (because we checked the format above) and put a 1 in the return string.
   retVal = ""
   For i = 1 To Len(value1)
      If Mid(value1, i, 1) = "0" And Mid(value2, i, 1) = "0" Then
         retVal = retVal & "0"
      Else ' One or both is a "1"
         retVal = retVal & "1"
      End If

   ExclusiveOr = retVal

   Exit Function
   Error Err, Error$ & Chr$(10) & "in function " & Getthreadinfo(1) & ", line " & Cstr(Erl)
End Function