Results 1 to 16 of 16

Thread: What does Xor mean?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2009
    Posts
    72

    What does Xor mean?

    Code:
    NowPlaying = NowPlaying Xor True : PictureBox1.Invalidate()
    Is this like php?

    Code:
    $variable = ($variable2 = false) ? true : false
    ?

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    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.

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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)

  4. #4
    Hyperactive Member su ki's Avatar
    Join Date
    Oct 2007
    Posts
    354

    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

  5. #5
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

  6. #6
    Hyperactive Member su ki's Avatar
    Join Date
    Oct 2007
    Posts
    354

    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

  7. #7
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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)
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  9. #9
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  10. #10
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: What does Xor mean?

    Quote Originally Posted by su ki View Post
    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...

  11. #11
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  12. #12
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: What does Xor mean?

    So,

    If A xor B

    is the same as

    If A = True or B = True and A <> B
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  13. #13
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Mar 2009
    Posts
    72

    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'

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. NowPlaying = Not NowPlaying
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  16. #16
    Hyperactive Member su ki's Avatar
    Join Date
    Oct 2007
    Posts
    354

    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
  •  



Click Here to Expand Forum to Full Width