• Welcome to Valhalla Legends Archive.
 

Help With XOR

Started by R.a.B.B.i.T, November 03, 2004, 06:57 PM

Previous topic - Next topic

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:    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


Quote 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?

Newby

#1
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

Quote[17:32:45] * xar sets mode: -oooooooooo algorithm ban chris cipher newby stdio TehUser tnarongi|away vursed warz
[17:32:54] * xar sets mode: +o newby
[17:32:58] <xar> new rule
[17:33:02] <xar> me and newby rule all

Quote<TehUser> Man, I can't get Xorg to work properly.  This sucks.
<torque> you should probably kill yourself
<TehUser> I think I will.  Thanks, torque.

MyndFyre

Newby is indeed correct.  You can't get around looking at the bits of a number when calculating XOR.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Adron

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)


Grok


Newby

#5
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 ... :'(
- Newby

Quote[17:32:45] * xar sets mode: -oooooooooo algorithm ban chris cipher newby stdio TehUser tnarongi|away vursed warz
[17:32:54] * xar sets mode: +o newby
[17:32:58] <xar> new rule
[17:33:02] <xar> me and newby rule all

Quote<TehUser> Man, I can't get Xorg to work properly.  This sucks.
<torque> you should probably kill yourself
<TehUser> I think I will.  Thanks, torque.

R.a.B.B.i.T

#6
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?

Newby

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"
- Newby

Quote[17:32:45] * xar sets mode: -oooooooooo algorithm ban chris cipher newby stdio TehUser tnarongi|away vursed warz
[17:32:54] * xar sets mode: +o newby
[17:32:58] <xar> new rule
[17:33:02] <xar> me and newby rule all

Quote<TehUser> Man, I can't get Xorg to work properly.  This sucks.
<torque> you should probably kill yourself
<TehUser> I think I will.  Thanks, torque.

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).

Adron

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?

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
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

Adron

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

Grok

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.

Adron

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?

Grok

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).