|
-
Apr 7th, 2009, 02:49 PM
#1
Thread Starter
Lively Member
What does Xor mean?
Code:
NowPlaying = NowPlaying Xor True : PictureBox1.Invalidate()
Is this like php?
Code:
$variable = ($variable2 = false) ? true : false
?
-
Apr 7th, 2009, 03:03 PM
#2
Re: What does Xor mean?
http://msdn.microsoft.com/en-us/libr...a6(VS.80).aspx
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Debug.WriteLine("---Xor---")
Debug.WriteLine(False Xor False) 'False
Debug.WriteLine(False Xor True) 'True
Debug.WriteLine(True Xor False) 'True
Debug.WriteLine(True Xor True) 'False
Debug.WriteLine("---Or---")
Debug.WriteLine(False Or False) 'False
Debug.WriteLine(False Or True) 'True
Debug.WriteLine(True Or False) 'True
Debug.WriteLine(True Or True) 'True
End Sub
I believe the "X" actually stands for Exclusion.
-
Apr 7th, 2009, 03:05 PM
#3
Re: What does Xor mean?
Xor returns True if just one of the conditions is True
As such, NowPlaying = NowPlaying Xor True is the same as this:
Code:
If NowPlaying = True Then
NowPlaying = False
Else
NowPlaying = True
End If
or this:
Code:
NowPlaying = Not(NowPlaying)
-
Apr 7th, 2009, 03:36 PM
#4
Hyperactive Member
Re: What does Xor mean?
Xor returns True if just one of the conditions is True
and returns False when both conditions are Same whether True or False
Code:
Condition1 Condition2 Result
True True False ---> note this result
False False False
True False True
False True True
the above is same what ForumAccount has told its just the another way to show u the working of XOR
* If my post helped you, please Rate it
* If your problem is solved please also mark the thread resolved it is there in right top of page under thread tools
* Why Rating is useful
-
Apr 7th, 2009, 03:36 PM
#5
Re: What does Xor mean?
a lot of times where xor is handy (where you will see it used a lot), is when you are trying to parse a combined flags enum to get individual values.
-
Apr 7th, 2009, 03:38 PM
#6
Hyperactive Member
Re: What does Xor mean?
a lot of times where xor is handy (where you will see it used a lot), is when you are trying to parse a combined flags enum to get individual values.
hey kleinma
please provide a sample for the same
* If my post helped you, please Rate it
* If your problem is solved please also mark the thread resolved it is there in right top of page under thread tools
* Why Rating is useful
-
Apr 7th, 2009, 03:38 PM
#7
Re: What does Xor mean?
XOR is actually eXclusive-OR.
One and only one conditions of the two must be true to make it return true. i.e. one is true and other is false.
Besides XORing two boolean expressions you can XOR two numbers also and it would return a number with their individual bits XORed.
Have a look here to know more about it.
-
Apr 7th, 2009, 03:57 PM
#8
Re: What does Xor mean?
@steel - what they are doing is toggling NowPlaying between true and false. A clearer way to do that is using Not
Code:
Dim toggle As Boolean
toggle = False
Debug.WriteLine(toggle.ToString)
toggle = toggle Xor True
Debug.WriteLine(toggle.ToString)
toggle = toggle Xor True
Debug.WriteLine(toggle.ToString)
toggle = Not toggle
Debug.WriteLine(toggle.ToString)
toggle = Not toggle
Debug.WriteLine(toggle.ToString)
-
Apr 7th, 2009, 04:13 PM
#9
Re: What does Xor mean?
Code:
Enum bi As Byte
t0 = CByte(2 ^ 0)
t1 = CByte(2 ^ 1)
t2 = CByte(2 ^ 2)
t3 = CByte(2 ^ 3)
t4 = CByte(2 ^ 4)
t5 = CByte(2 ^ 5)
t6 = CByte(2 ^ 6)
t7 = CByte(2 ^ 7)
tAll = &HFF
End Enum
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'sample data
Dim flags As Byte = bi.t0 Or bi.t3 Or bi.t4 Or bi.t6 'turn some bits on for example
'create a mask
Dim TwoBitMask As Byte = bi.tAll Xor (bi.t3 Or bi.t4)
'turn only bit 3 & 4 off
flags = flags And TwoBitMask
End Sub
-
Apr 7th, 2009, 04:18 PM
#10
Re: What does Xor mean?
 Originally Posted by su ki
hey kleinma
please provide a sample for the same
Sure... lets use the keys enum as an example (because I just had to do this the other day)
so keys is an enum value at system.windows.forms.keys
the enum has all the different values for the possible keys on a keyboard that windows would recognize, and each enum member has its own value that corresponds to the given key.
So
keys.a (the a key) has a value of 65
keys.esc (the escape key) has a value of 27
keys.shiftkey (the shift key) has a value of 16
but the actual modifier of the shift key being pressed is
keys.shift which has a value of 65536
anyways, the keys enumeration (any enumeration that has the <flags> attribute on it), can be used for bitwise combination.
This means I could say
Code:
Dim myKeys as Keys = Keys.shift or keys.a
using or is how you combine bits, so the value of myKeys would be both the shift modifier key and the a key.
So the problem is, how do I know that?
Well if I want to test if the shift key was pressed along with whatever other key i pressed, it is easy enough to write code like this with an or keyword which will return true if myKeys contains shift (regardless of what other keys it contains)
Code:
if myKeys = (myKeys or keys.shift) then
'myKeys contains shift modifier
else
'myKeys does not contain shift modifier
end if
so you can say, ok great, I know the shift modifier key was pressed, but what actual key was pressed along with it? well you could write a huge long if statement using or to test every possible key combination, or you could use xor like this:
Code:
Dim KeyPressedAlongWithShift as keys = CType((myKeys Xor keys.shift), Keys)
and the value of KeyPressedAlongWithShift would be 65 also known as keys.a
I hope that makes sense.. bit shifting can be a bit confusing...
-
Apr 7th, 2009, 04:30 PM
#11
Re: What does Xor mean?
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
i = 42
i = -i 'i=negative 42
'two-complement the hard way
i = 42
i = (i Xor &HFFFFFFFF) + 1 'i=negative 42
End Sub
Last edited by dbasnett; Apr 7th, 2009 at 05:12 PM.
-
Apr 7th, 2009, 04:44 PM
#12
Re: What does Xor mean?
So,
If A xor B
is the same as
If A = True or B = True and A <> B
-
Apr 7th, 2009, 05:31 PM
#13
Re: What does Xor mean?
In terms of Boolean logic yes, but it would usually be written more like this:
Code:
If (A = True And B = False) or (A = False And B = True) Then
As such, the original code (which had a hard-coded 'True') was basically the same as this:
Code:
A = (A = True And True = False) or (A = False And True = True)
..because the first half will always evaluate to False, it can be reduced to this:
Code:
A = (A = False And True = True)
..and because the second half of that will always evaluate to True, it can then be reduced to either of these:
Code:
A = (A = False)
A = Not(A)
The bitwise uses are more complex, but are based on the same principles.
-
Apr 7th, 2009, 05:59 PM
#14
Thread Starter
Lively Member
Re: What does Xor mean?
A million thanks..
will remember this tool, its the same as PHP
except its
($var = true) ? 'var is true' : 'var is false'
-
Apr 7th, 2009, 07:23 PM
#15
Re: What does Xor mean?
I will just point out that if you want to toggle each bit of a value then using Xor makes sense but if you want to toggle a boolean value then it makes far more sense to simply negate it:
vb.net Code:
NowPlaying = Not NowPlaying
-
Apr 8th, 2009, 12:16 AM
#16
Hyperactive Member
Re: What does Xor mean?
hey kleinma
thanks for the example :-)
* If my post helped you, please Rate it
* If your problem is solved please also mark the thread resolved it is there in right top of page under thread tools
* Why Rating is useful
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|