Results 1 to 14 of 14

Thread: [RESOLVED] Understanding XOR

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Resolved [RESOLVED] Understanding XOR

    I am trying to understand how XOR works. Why is this equal to 25?
    Code:
        MsgBox 77 Xor 84
    Just testing this:
    Code:
    Public Sub Main()
        Dim intFix As Integer
        intFix = Fix(256 * Rnd)
        Range("A1").Value = intFix
        
        Dim intAsc As Integer
        intAsc = Asc(Mid$("Test", 1, 1))
        Range("A2").Value = intAsc
        
        Range("A3").Value = intFix Xor intAsc
        
        MsgBox 77 Xor 84
    End Sub
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

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

    Re: Understanding XOR

    XOR does a binary comparison - each bit from the numbers is compared, and if either one but not both is true (ie: the bit in one number doesn't equal the bit in the other), the result is a 1.

    In binary (thanks Calculator!), 77 is 1001101, and 84 is 1010100.

    1001101 <-77
    1010100 <-84
    0011001 <-Xor (where the bits differ)

    ..and when converted back to decimal, that value is 25

    You can do similar with Or (if either bit is 1, the output is 1), or And (if both bits are 1, the output is 1).


    Based on your recent posts, I presume this is to do with encryption - and the reason that Xor is used is that you can 'undo' the encryption by Xor'ing with the known value.

    Assuming 77 was the value you are encrypting, 84 is your 'known value', so Xor'ing the result (25) with it actually gets your original value back - but Xor'ing it with anything else will get the wrong value.

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Understanding XOR

    Much better explanation than mine which follows only because I already prepared it.

    Code:
    Bit positions from left to right
    1 2 4 8 16 32 64
    x   x x       x  = 77
        x    x    x  = 84
    
    Taking 77 and XORing it means to add bits if not there or remove if they are there
    so 84 laying over 77 while adding would look like following: o=removed, +=added, x=unchanged
    1 2 4 8 16 32 64
    x   o x  +     o  = 25
    And finally, adding these up, we get 1 Or 8 Or 16 which = 25
    ORing adds bits if they were not there already
    Last edited by LaVolpe; Nov 28th, 2007 at 06:00 PM.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: Understanding XOR

    Excellent explanation. I been searching the web for a clear explanation like this but did not get any.

    Quote Originally Posted by si_the_geek
    You can do similar with Or (if either bit is 1, the output is 1), or And (if both bits are 1, the output is 1).
    Always the additional stuff that is good to know.
    Quote Originally Posted by si_the_geek
    Based on your recent posts, I presume this is to do with encryption - and the reason that Xor is used is that you can 'undo' the encryption by Xor'ing with the known value.
    Yes, I really want to understand that CVMichael Dude's code.

    College taught me to convert EBCDIC, Binary and DEC without a calculator. Can you direct me to a tutorial because I asked a similar question last week.

    Thank You Si
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: Understanding XOR

    Quote Originally Posted by LaVolpe
    Much better explanation than mine which follows only because I already prepared it.

    Code:
    Bit positions from left to right
    1 2 4 8 16 32 64
    x   x x       x  = 77
        x    x    x  = 84
    
    Taking 77 and XORing it means to add bits if not there or remove if they are there
    so 84 laying over 77 while adding would look like following: o=removed, +=added, x=unchanged
    1 2 4 8 16 32 64
    x   o x  +     o  = 25
    And finally, adding these up, we get 1 Or 8 Or 16 which = 25
    ORing adds bits if they were not there already
    I think this might answer my previous question. Let me take a look at it and get back to you if I have any additional questions. Thank you!
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: Understanding XOR

    LaVolpe, that is excellent. With that demo, I can compare AND, OR and so on just like Si_The_Geek mention. Now, I understand how you got to the 25.

    But, I am still interest to understand how 25 is converted from binary 0011001 without using a calculator.
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

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

    Re: Understanding XOR

    The bit on the right (when set to 1) has a decimal value of 1, and each bit to the left of it doubles in value - so you simply add the value of the bits, eg:
    Code:
    0  0  1  1 0 0 1
    64 32 16 8 4 2 1
    so, 16+8+1 = 25

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: Understanding XOR

    cool!

    I actually found these two articles:
    http://www.wikihow.com/Convert-from-Decimal-to-Binary
    http://www.wikihow.com/Convert-from-Binary-to-Decimal

    Thanks for getting me going on this Si_The_Geek and LaVolpe!
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: [RESOLVED] Understanding XOR

    One last question, when I convert binary to decimal, do i convert binary to hex or decimal to hex?
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  10. #10
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: [RESOLVED] Understanding XOR

    Quote Originally Posted by Liquid Metal
    One last question, when I convert binary to decimal, do i convert binary to hex or decimal to hex?

    It doesn't matter you can convert from Decimal to Octal to Binary to Hex and back to Octal if you want or any combination in between. You don't need to be in one particular base inorder to convert to another base.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  11. #11
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: [RESOLVED] Understanding XOR

    You don't really convert anything. They are the same value represented differently.

    Binary = 2 base
    Decimal = 10 base
    Hex = 16 base

    In VB code you can't write bits directly, but decimals and hex are supported. When you write...
    Code:
    lngSomething = 10
    lngSomething = &HA
    ... you do the exact same thing. There is no difference in the final compiled code and there is no executional difference. The value is just represented in different base. If binary representation would be supported, the end result would be the same with it.

    Basically one uses hex values in VB code to ease understanding the code... like, when you see F, you know all four bits are being active.


    Code:
    ' only highest bit remains active if it is there
    bytSomething = bytSomething And &H80
    
    ' only high order bits remain active
    bytSomething = bytSomething And &HF0
    
    ' only low order bits remain active
    bytSomething = bytSomething And &H0F
    
    ' all except the highest bit remain active
    bytSomething = bytSomething And &H7F
    Last edited by Merri; Nov 28th, 2007 at 07:02 PM.

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: [RESOLVED] Understanding XOR

    Thanks

    I actually found this site and it make so much sense now, especially looking at the last table.

    http://www.permadi.com/tutorial/numHexToBin/index.html

    Merri, I noticed an "H" in your code. Hex only goes up to "F". Is the "H" a VB representation of Hex format?
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

  13. #13
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: [RESOLVED] Understanding XOR

    &H tells the compiler the following values are hex numbers.

  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2003
    Location
    The Future - Skynet
    Posts
    1,157

    Re: [RESOLVED] Understanding XOR

    Thank You
    I'll Be Back!

    T-1000

    Microsoft .Net 2005
    Microsoft Visual Basic 6
    Prefer using API

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