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:
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 iQuote from: Output0 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
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 from: Output 20 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
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?
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.
Newby is indeed correct. You can't get around looking at the bits of a number when calculating XOR.
Quote from: R.a.B.B.i.T on November 03, 2004, 06:57 PM
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.
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 from: Grok on November 04, 2004, 06:55 AM
Quote from: Newby on November 03, 2004, 09:07 PM
i.e
01011100
01001010
-----------
11101001
!!!!
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 ... :'(
Quote from: Adron on November 03, 2004, 11:41 PM
Quote from: R.a.B.B.i.T on November 03, 2004, 06:57 PM
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.
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)
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?
Quote from: R.a.B.B.i.T on November 04, 2004, 05:42 PM
@The example:
#01011100
#01001010
-----------
#11101001
$5c
$4a
------
$e9
90
74
-----
213
Correct?
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"
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).
Quote from: Newby on November 04, 2004, 06:10 PM
Quote from: R.a.B.B.i.T on November 04, 2004, 05:42 PM
@The example:
#01011100
#01001010
-----------
#11101001
1. Incorrect You're doing AND on the bits.
He's not. He's doing XNOR?
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 from: Dyndrilliac on November 05, 2004, 07:16 AM
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
If he were doing AND, the two zeros would have resulted in a zero instead of a one too...
0101101011
0110100101
----------------
0100100001
Quote from: Grok on November 04, 2004, 06:55 AM
Quote from: Newby on November 03, 2004, 09:07 PM
i.e
01011100
01001010
-----------
11101001
!!!!
I cannot figure what it was. It starts out like XNOR for 7 bits, then the last bit messes that up.
Quote from: Grok on November 05, 2004, 09:29 AM
Quote from: Grok on November 04, 2004, 06:55 AM
Quote from: Newby on November 03, 2004, 09:07 PM
i.e
01011100
01001010
-----------
11101001
!!!!
I cannot figure what it was. It starts out like XNOR for 7 bits, then the last bit messes that up.
Last bit? 0 and 0 should be 1 for XNOR, just like the first?
Quote from: Adron on November 05, 2004, 11:26 AM
Quote from: Grok on November 05, 2004, 09:29 AM
Quote from: Grok on November 04, 2004, 06:55 AM
Quote from: Newby on November 03, 2004, 09:07 PM
i.e
01011100
01001010
-----------
11101001
!!!!
I cannot figure what it was. It starts out like XNOR for 7 bits, then the last bit messes that up.
Last bit? 0 and 0 should be 1 for XNOR, just like the first?
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).
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 from: R.a.B.B.i.T on November 05, 2004, 04:36 PM
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?
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)
I do want to rewrite it for some reason! My original equation was just as you said, but resulted in low negatives: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 YThis yeilded -60, as opposed to 0 (which the Xor operator gave me). I believe that Visual BASIC has a thing against me. :'(
Quote from: R.a.B.B.i.T on November 06, 2004, 10:13 AM
I do want to rewrite it for some reason! My original equation was just as you said, but resulted in low negatives: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 YThis yeilded -60, as opposed to 0 (which the Xor operator gave me). I believe that Visual BASIC has a thing against me. :'(
That's strange. My result is of course 0:
?((not 5) and 5) or (5 and not 5)
0
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.