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
Printable View
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
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!
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
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
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.
I am slowly getting to grips with it
But still the binary to integers is confusing
Could you try again??
Thanks alot
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.
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.
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:D
VB Code:
Public Function ToBinary(ByVal lIn As Long) As String Dim sBinary As String Dim nIndex As Long Dim lMask As Long For nIndex = 0 To 30 lMask = 2# ^ nIndex If (lIn And lMask) Then sBinary = "1" & sBinary Else sBinary = "0" & sBinary End If Next nIndex If lIn < 0 Then sBinary = "1" & sBinary Else sBinary = "0" & sBinary End If ToBinary = sBinary End Function
Anyone want to have a go at getting from a int to a hex??
Hex() :D ;)
Thats wasted on meQuote:
Originally posted by plenderj
Hex() :D ;)
Try
VB Code:
Debug.Print Hex(10)
I have seen the light
Thank you all
You have been most helpful
And to get from Hex back to int:For example:VB Code:
CInt(&H<HEX_VAL>&)VB Code:
Hex(10) = A CInt(&HA&) = 10 Hex(510) = 1FE CInt(&H1FE&) = 510
Convert Binary Values To Decimal ValuesQuote:
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 Decimal Values To Binary Values
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.
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:)
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.
Ok then chaps sorry to keep banging on about this, but its getting quite interesting now:p
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
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.
Don't suppose you have any examples of the two
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
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.