Results 1 to 25 of 25

Thread: Xor

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    37

    Xor

    I know this has been covered before I have searched the archives

    But I still don't understand this!!

    Why does v = 9 Xor 10 = 3???

    Can someone please explain this very slowly for me

    I am having a bad day with this

    Thanks in advance

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Xor takes two numbers and converts them to binary. Then in compares the "zeros" and the "ones". If they are the same, you get a new value of 0 and if they are different you get 1.

    So:
    9 = 1 0 0 1
    10 = 1 0 1 0
    ----------------
    v = 0 0 1 1

    And convering back you get 3! So 9 Xor 10 and 10 Xor 9 gives you the same result.

    Hope your day gets better!
    Last edited by manavo11; Apr 2nd, 2003 at 09:19 AM.


    Has someone helped you? Then you can Rate their helpful post.

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    37
    Great thanx alot that has cleared thingsup

    I never thought I was going to get this one

    One thing though is there a conversion function in vb where I can convert numbers to binary to have a look at what is going on myself?


    Thanks again

  4. #4
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    0 xor 0 = 0
    0 xor 1 = 1
    1 xor 0 = 1
    1 xor 1 = 0

    Since the high-order bit is 1 in both values, they = 0 in the result. This leaves bit 1 (= 1 in 9 and = 0 in 10) and bit 2 (= 0 in 9 and = 1 in 10) which will both = 1 in the result.

    10 - 1010
    09 - 1001
    ---------
    03 - 0011

  5. #5
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    I am not sure. I used my mind because it was with simple numbers. Making binary to an integer : starting from right to left the right character is 2^0(to the power of 0) and the second from the right end is 2^1 and so on. Now you can multiply that by zero or one(depending on the digit of binary) and add them all up or something like that. I am not sure I am explaining well so if you don't don't understand(wich you probably won't) please tell me to try to explain better.


    Has someone helped you? Then you can Rate their helpful post.

  6. #6

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    37
    I am slowly getting to grips with it

    But still the binary to integers is confusing

    Could you try again??

    Thanks alot

  7. #7
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    A (hopefully) simple explanation:

    Suppose, you have the number 2345, which is a normal, decimal integer.
    2345 =
    5 + 40 + 300 + 2000 =
    5 * 1 + 4 * 10 + 3 * 100 + 2 * 1000 =
    5 * 10^0 + 4 * 10^1 + 3 * 10^2 + 2 * 10^3

    The value of each digit is influenced by the position. The more to the left, the higher the value. The rightmost digit has to be multiplied with 10 ^ 0 (= 1). For each digit to the left the exponent of the power is increased by one: 1 -> 10 -> 100 -> 1000, etc.

    For binary values (or octal or hexadecimal) it is the same. Only, the base (called "radix") isn't 10, but 2 (or 8, or 16).
    So, to convert the binary value 100101 to a decimal value, you have to add 1 * 2^0 + 0 * 2^1 + 1 * 2^2 + 0 * 2^3 + 0 * 2^4 + 1 * 2^5. For sake of ease, you can simply skip the zero-values. This will result in 1 * 1 + 1 * 4 + 1 * 32, which sums up as 37.

  8. #8
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    Oh, and coming back to the Xor-operator:
    9 Xor 10 = 3, 10 Xor 9 = 3.
    But: 9 Xor 3 = 10, 3 Xor 9 = 10.
    And 10 Xor 3 = 9, 3 Xor 10 = 9.

    Because Xor is reversible, it is used in many (simple) encryption algorithms.

    There is no normal numerical relation between the values you give in and the result, it is purely binary as manavo11 explained.

  9. #9

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    37
    Thanks alot for all of yyour replies

    I have managed to get my head around with this

    37 would be

    0 0 1 0 0 1 0 1
    128 ,64, 32, 16 ,8 ,4, 2, 1

    adding 32+4+1

  10. #10
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    VB Code:
    1. Public Function ToBinary(ByVal lIn As Long) As String
    2.  
    3. Dim sBinary As String
    4. Dim nIndex As Long
    5. Dim lMask As Long
    6.  
    7. For nIndex = 0 To 30
    8.     lMask = 2# ^ nIndex
    9.     If (lIn And lMask) Then
    10.         sBinary = "1" & sBinary
    11.     Else
    12.         sBinary = "0" & sBinary
    13.     End If
    14. Next nIndex
    15. If lIn < 0 Then
    16.     sBinary = "1" & sBinary
    17. Else
    18.     sBinary = "0" & sBinary
    19. End If
    20.  
    21. ToBinary = sBinary
    22.  
    23. End Function
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  11. #11

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    37
    Anyone want to have a go at getting from a int to a hex??

  12. #12
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Hex()
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  13. #13

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    37
    Originally posted by plenderj
    Hex()
    Thats wasted on me

  14. #14
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    Try
    VB Code:
    1. Debug.Print Hex(10)
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  15. #15

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    37
    I have seen the light


    Thank you all

    You have been most helpful

  16. #16
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357
    And to get from Hex back to int:
    VB Code:
    1. CInt(&H<HEX_VAL>&)
    For example:
    VB Code:
    1. Hex(10) = A
    2. CInt(&HA&) = 10
    3.  
    4. Hex(510) = 1FE
    5. CInt(&H1FE&) = 510
    ~seaweed

  17. #17
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Originally posted by DirkDiggler
    One thing though is there a conversion function in vb where I can convert numbers to binary to have a look at what is going on myself?
    Convert Binary Values To Decimal Values

    Convert Decimal Values To Binary Values


    Has someone helped you? Then you can Rate their helpful post.

  18. #18
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104
    And just for your entertainment, remember that the calculator bundled with all versions of Windows has the means to convert decimal to hex to binary, etc, and you can do some logical calculations with it. I find it very handy when flipping bits in bitmasks.

  19. #19

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    37
    Yes I have been using the calculator to confirm things as I have been going along.

    I would like to thank everyone again you have all been very helpful on a topic I thought I would never really get to grips with, but feel quite comfortable with now.


    Thanks again

  20. #20
    Lively Member
    Join Date
    Mar 2003
    Location
    Cardiff, UK
    Posts
    67
    I wrote a bitmask calculator in php, which is here.

    I find it quite handy at times when I want to know what specific bits are set, rather than just what the equivalent binary/decimal number is.

  21. #21

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    37
    Ok then chaps sorry to keep banging on about this, but its getting quite interesting now

    With my newly found understanding of Xor what can I do with it

    A little bit of encryption perhaps?

    Does anyone fancy showing me how?

    ps.Whats this triple des thing all about?


    Thanks again
    Last edited by DirkDiggler; Apr 3rd, 2003 at 02:51 AM.

  22. #22
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Xor provides very weak encryption.

    And triple DES is the DES algorithm run three times over your data.
    Its a 56bit encryption algorithm if I remember correctly.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  23. #23

    Thread Starter
    Member
    Join Date
    Jun 2002
    Posts
    37
    Don't suppose you have any examples of the two

  24. #24
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    I think CVMichael has a post in the codebank. If not in the codebank I believe I have seen some posts by him around. Try searching


    Has someone helped you? Then you can Rate their helpful post.

  25. #25
    Lively Member Tygur's Avatar
    Join Date
    Jul 2002
    Posts
    108
    I know I'm resurrecting an old thread here, and I don't normally like to do that, but I wanted to mention that it's not just simple weak encryption methods that use Xor.

    As an example, Blowfish, a very well known and respected encryption algorithm, uses Xor.

    Also, Xor is the basis for the one time pad, which, while difficult to implement correctly, is very simple and has been called unbreakable.

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