Valhalla Legends Forums Archive | Visual Basic Programming | Help With XOR

AuthorMessageTime
R.a.B.B.i.T
Google and MSDN didn't yeild much to me about XOR when used with numerical expressions.  I understand how it works with boolean values (thanks to my electronics course), but I still can't figure out how XOR works.  I tried to replicate how XOR works by using the formula (Y = (!A)B + A(!B)), but that didn't turn out to well.  I used the following code to generate the output below:[code]    Dim i As Integer
    For i = 0 To 100 Step 5
        Debug.Print CStr(i & " XOR 5 = " & ((Not i) * 5) + (i * (Not 5)))
        Debug.Print CStr(i & " Xor 5 = " & (i Xor 5))
        Debug.Print vbNewLine
    Next i[/code]

[quote="Output"]0 XOR 5 = -5
0 Xor 5 = 5

5 XOR 5 = -60
5 Xor 5 = 0

10 XOR 5 = -115
10 Xor 5 = 15

15 XOR 5 = -170
15 Xor 5 = 10

20 XOR 5 = -225
20 Xor 5 = 17

25 XOR 5 = -280
25 Xor 5 = 28

30 XOR 5 = -335
30 Xor 5 = 27

35 XOR 5 = -390
35 Xor 5 = 38

40 XOR 5 = -445
40 Xor 5 = 45

45 XOR 5 = -500
45 Xor 5 = 40

50 XOR 5 = -555
50 Xor 5 = 55

55 XOR 5 = -610
55 Xor 5 = 50

60 XOR 5 = -665
60 Xor 5 = 57

65 XOR 5 = -720
65 Xor 5 = 68

70 XOR 5 = -775
70 Xor 5 = 67

75 XOR 5 = -830
75 Xor 5 = 78

80 XOR 5 = -885
80 Xor 5 = 85

85 XOR 5 = -940
85 Xor 5 = 80

90 XOR 5 = -995
90 Xor 5 = 95

95 XOR 5 = -1050
95 Xor 5 = 90

100 XOR 5 = -1105
100 Xor 5 = 97[/quote]Where XOR is my calculation [(Not i) * 5) + (i * (Not 5)] and Xor is Visual BASIC's Xor operator.

When, however, I change my equation to [(Not i) * 5) - (i * (Not 5)], my output is the following:[quote="Output 2"]0 XOR 5 = -5
0 Xor 5 = 5

5 XOR 5 = 0
5 Xor 5 = 0

10 XOR 5 = 5
10 Xor 5 = 15

15 XOR 5 = 10
15 Xor 5 = 10

20 XOR 5 = 15
20 Xor 5 = 17

25 XOR 5 = 20
25 Xor 5 = 28

30 XOR 5 = 25
30 Xor 5 = 27

35 XOR 5 = 30
35 Xor 5 = 38

40 XOR 5 = 35
40 Xor 5 = 45

45 XOR 5 = 40
45 Xor 5 = 40

50 XOR 5 = 45
50 Xor 5 = 55

55 XOR 5 = 50
55 Xor 5 = 50

60 XOR 5 = 55
60 Xor 5 = 57

65 XOR 5 = 60
65 Xor 5 = 68

70 XOR 5 = 65
70 Xor 5 = 67

75 XOR 5 = 70
75 Xor 5 = 78

80 XOR 5 = 75
80 Xor 5 = 85

85 XOR 5 = 80
85 Xor 5 = 80

90 XOR 5 = 85
90 Xor 5 = 95

95 XOR 5 = 90
95 Xor 5 = 90

100 XOR 5 = 95
100 Xor 5 = 97[/quote]The second equation is CLOSER but still not exact.  I have no idea how to replicate XOR at this point (with numerical arguments), though my boolean model works just fine (same model as the first, but with boolean values instead of integers).

I am having a *lot* of trouble with this, mainly because it's annoying.  Does anyone know about Xor?
November 4, 2004, 12:57 AM
Newby
xor looks at the bits of two numbers. If one of them is 1, and the other is 0, the result is 1. If both are 0 or both are 1, the result is 0.

Number 1: 1
Number 2: 0
Result: 1

Number 1: 1
Number 2: 1
Result: 0

Number 1: 0
Number 2: 1
Result: 1

Number 1: 0
Number 2: 0
Result: 0

i.e

01011100
01001010
-----------
00010110

That's just off the top of my head. I could be wrong. Only reason I remember this is because if you xor a number by itself, it returns 0. :)

EDIT -- I'm not sure if you need help with the xor operator in general, or writing your own xor function.
November 4, 2004, 3:07 AM
Myndfyr
Newby is indeed correct.  You can't get around looking at the bits of a number when calculating XOR.
November 4, 2004, 3:23 AM
Adron
[quote author=R.a.B.B.i.T link=topic=9414.msg87268#msg87268 date=1099529821]
Google and MSDN didn't yeild much to me about XOR when used with numerical expressions.  I understand how it works with boolean values (thanks to my electronics course), but I still can't figure out how XOR works.  I tried to replicate how XOR works by using the formula (Y = (!A)B + A(!B)), but that didn't turn out to well. 
[/quote]

Replicating it using (!A)B + A(!B) should work well, if you use the appropriate operators; ! = NOT, * = AND, + = OR (as I'm sure you learned in your electronics course):

A XOR B = ((NOT A) AND B) OR (A AND NOT B)

November 4, 2004, 5:41 AM
Grok
[quote author=Newby link=topic=9414.msg87284#msg87284 date=1099537667]
i.e

01011100
01001010
-----------
11101001[/quote]


!!!!
November 4, 2004, 12:55 PM
Newby
[quote author=Grok link=topic=9414.msg87329#msg87329 date=1099572940]
[quote author=Newby link=topic=9414.msg87284#msg87284 date=1099537667]
i.e

01011100
01001010
-----------
11101001[/quote]


!!!!
[/quote]
Ooh shit. Caught that, and corrected. Thanks Grok. Wow, that was stupid of me. :(

Even after giving the right description I still manage to fuck it up ... :'(
November 4, 2004, 2:17 PM
R.a.B.B.i.T
[quote author=Adron link=topic=9414.msg87319#msg87319 date=1099546861]
[quote author=R.a.B.B.i.T link=topic=9414.msg87268#msg87268 date=1099529821]
Google and MSDN didn't yeild much to me about XOR when used with numerical expressions.  I understand how it works with boolean values (thanks to my electronics course), but I still can't figure out how XOR works.  I tried to replicate how XOR works by using the formula (Y = (!A)B + A(!B)), but that didn't turn out to well. 
[/quote]

Replicating it using (!A)B + A(!B) should work well, if you use the appropriate operators; ! = NOT, * = AND, + = OR (as I'm sure you learned in your electronics course):

A XOR B = ((NOT A) AND B) OR (A AND NOT B)


[/quote]Indeed I did, that's why I was able to replicate the boolean XOR with no problem, but should I convert the number to binary first, do the XOR, then convert it back?

@Newby: a little help with XORing numbers (since I didn't learn anything but XORing bools), but mostly writing my own XOR function that will work with numerical arguments.

@The example:
#01011100
#01001010
-----------
#11101001

$5c
$4a
------
$e9

90
74
-----
213

Correct?
November 4, 2004, 11:42 PM
Newby
[quote author=R.a.B.B.i.T link=topic=9414.msg87403#msg87403 date=1099611759]
@The example:
#01011100
#01001010
-----------
#11101001

$5c
$4a
------
$e9

90
74
-----
213

Correct?
[/quote]

1. Incorrect You're doing AND on the bits.

2. Wrong. 0x5C ^ 0x4A == 0x16

3. Wrong. 90 ^ 74 == 16

And I'm unsure if this is what you want, as I didn't understand what you meant by "numerical arguments"
November 5, 2004, 12:10 AM
R.a.B.B.i.T
Numerical vs boolean.  Bools are easy since it's only 1 bit.  The bytes/nibbles/words/whatever are what throw me off.

Also, with the hex and decimal values: I just converted them from the binary, but I obviously failed (which is bad, because I have a test on these conversions tomorrow).
November 5, 2004, 1:48 AM
Adron
[quote author=Newby link=topic=9414.msg87414#msg87414 date=1099613419]
[quote author=R.a.B.B.i.T link=topic=9414.msg87403#msg87403 date=1099611759]
@The example:
#01011100
#01001010
-----------
#11101001
[/quote]

1. Incorrect You're doing AND on the bits.

[/quote]

He's not. He's doing XNOR?
November 5, 2004, 8:21 AM
Dyndrilliac
Newby was saying whatn he was doing (AND) is wrong. If he were doing XOR, the two zeros would have resulted in a zero instead of a one.


0101101011
0110100101
----------------
0011001110
November 5, 2004, 1:16 PM
Adron
[quote author=Dyndrilliac link=topic=9414.msg87494#msg87494 date=1099660615]
Newby was saying whatn he was doing (AND) is wrong. If he were doing XOR, the two zeros would have resulted in a zero instead of a one.


0101101011
0110100101
----------------
0011001110
[/quote]

If he were doing AND, the two zeros would have resulted in a zero instead of a one too...

0101101011
0110100101
----------------
0100100001
November 5, 2004, 2:00 PM
Grok
[quote author=Grok link=topic=9414.msg87329#msg87329 date=1099572940]
[quote author=Newby link=topic=9414.msg87284#msg87284 date=1099537667]
i.e

01011100
01001010
-----------
11101001[/quote]


!!!!
[/quote]

I cannot figure what it was.  It starts out like XNOR for 7 bits, then the last bit messes that up.
November 5, 2004, 3:29 PM
Adron
[quote author=Grok link=topic=9414.msg87501#msg87501 date=1099668597]
[quote author=Grok link=topic=9414.msg87329#msg87329 date=1099572940]
[quote author=Newby link=topic=9414.msg87284#msg87284 date=1099537667]
i.e

01011100
01001010
-----------
11101001[/quote]


!!!!
[/quote]

I cannot figure what it was.  It starts out like XNOR for 7 bits, then the last bit messes that up.
[/quote]

Last bit? 0 and 0 should be 1 for XNOR, just like the first?
November 5, 2004, 5:26 PM
Grok
[quote author=Adron link=topic=9414.msg87513#msg87513 date=1099675616]
[quote author=Grok link=topic=9414.msg87501#msg87501 date=1099668597]
[quote author=Grok link=topic=9414.msg87329#msg87329 date=1099572940]
[quote author=Newby link=topic=9414.msg87284#msg87284 date=1099537667]
i.e

01011100
01001010
-----------
11101001[/quote]


!!!!
[/quote]

I cannot figure what it was.  It starts out like XNOR for 7 bits, then the last bit messes that up.
[/quote]

Last bit? 0 and 0 should be 1 for XNOR, just like the first?
[/quote]

Oops, weird.  I looked at that 10 times and saw a 0 and 1 in the last column, every time!  (no pun about 10 times either).
November 5, 2004, 9:43 PM
R.a.B.B.i.T
Well, that WOULD change the converted values...
But I didn't want to do XNOR, I wanted to do XOR!  Arg?  This is getting tricky, mostly due to the fact that we all seemed to have messed up on the binary example.  But whatever.  Back to my earlier question (which I think I should do): should I convert the values I want to XOR into binary, do the actual XORing, and then convert back, or something else?
November 5, 2004, 10:36 PM
Adron
[quote author=R.a.B.B.i.T link=topic=9414.msg87545#msg87545 date=1099694212]
Well, that WOULD change the converted values...
But I didn't want to do XNOR, I wanted to do XOR!  Arg?  This is getting tricky, mostly due to the fact that we all seemed to have messed up on the binary example.  But whatever.  Back to my earlier question (which I think I should do): should I convert the values I want to XOR into binary, do the actual XORing, and then convert back, or something else?
[/quote]

If you just want to Xor values, you should be using the Xor operator....

If you want to rewrite it for some strange reason, you can use:

a Xor b = (a And Not b) Or (b And Not a)

November 6, 2004, 11:26 AM
R.a.B.B.i.T
I do want to rewrite it for some reason!  My original equation was just as you said, but resulted in low negatives:[code]Dim A As Integer
Dim B As Integer
Dim Y As Integer
A = 5
B = 5
Y = ((Not A) And B) Or (A And Not B)
Debug.Print Y[/code]This yeilded -60, as opposed to 0 (which the Xor operator gave me).  I believe that Visual BASIC has a thing against me. :'(
November 6, 2004, 4:13 PM
Adron
[quote author=R.a.B.B.i.T link=topic=9414.msg87629#msg87629 date=1099757584]
I do want to rewrite it for some reason!  My original equation was just as you said, but resulted in low negatives:[code]Dim A As Integer
Dim B As Integer
Dim Y As Integer
A = 5
B = 5
Y = ((Not A) And B) Or (A And Not B)
Debug.Print Y[/code]This yeilded -60, as opposed to 0 (which the Xor operator gave me).  I believe that Visual BASIC has a thing against me. :'(
[/quote]

That's strange. My result is of course 0:

?((not 5) and 5) or (5 and not 5)
0
November 7, 2004, 3:06 PM
R.a.B.B.i.T
Eh, VB seems not to like me.  I'll try variations on the original code and see if I can get it to work, but so far nothing I've done seems to have worked.
November 7, 2004, 3:50 PM

Search