Is this like php?Code:NowPlaying = NowPlaying Xor True : PictureBox1.Invalidate()
?Code:$variable = ($variable2 = false) ? true : false
Printable View
Is this like php?Code:NowPlaying = NowPlaying Xor True : PictureBox1.Invalidate()
?Code:$variable = ($variable2 = false) ? true : false
http://msdn.microsoft.com/en-us/libr...a6(VS.80).aspx
I believe the "X" actually stands for Exclusion.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
Xor returns True if just one of the conditions is True
As such, NowPlaying = NowPlaying Xor True is the same as this:
or this:Code:If NowPlaying = True Then
NowPlaying = False
Else
NowPlaying = True
End If
Code:NowPlaying = Not(NowPlaying)
and returns False when both conditions are Same whether True or FalseQuote:
Xor returns True if just one of the conditions is True
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
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 kleinmaQuote:
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.
please provide a sample for the same
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.
@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)
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
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
using or is how you combine bits, so the value of myKeys would be both the shift modifier key and the a key.Code:Dim myKeys as Keys = Keys.shift or keys.a
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)
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:if myKeys = (myKeys or keys.shift) then
'myKeys contains shift modifier
else
'myKeys does not contain shift modifier
end if
and the value of KeyPressedAlongWithShift would be 65 also known as keys.aCode:Dim KeyPressedAlongWithShift as keys = CType((myKeys Xor keys.shift), Keys)
I hope that makes sense.. bit shifting can be a bit confusing...
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
So,
If A xor B
is the same as
If A = True or B = True and A <> B
In terms of Boolean logic yes, but it would usually be written more like this:
As such, the original code (which had a hard-coded 'True') was basically the same as this:Code:If (A = True And B = False) or (A = False And B = True) Then
..because the first half will always evaluate to False, it can be reduced to this:Code:A = (A = True And True = False) or (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 And True = True)
The bitwise uses are more complex, but are based on the same principles.Code:A = (A = False)
A = Not(A)
A million thanks..
will remember this tool, its the same as PHP
except its
($var = true) ? 'var is true' : 'var is false'
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
hey kleinma
thanks for the example :-)