|
-
Nov 28th, 2007, 05:30 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] Understanding XOR
I am trying to understand how XOR works. Why is this equal to 25?
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
-
Nov 28th, 2007, 05:49 PM
#2
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.
-
Nov 28th, 2007, 05:56 PM
#3
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.
-
Nov 28th, 2007, 06:02 PM
#4
Thread Starter
Frenzied Member
Re: Understanding XOR
Excellent explanation. I been searching the web for a clear explanation like this but did not get any.
 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.
 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
-
Nov 28th, 2007, 06:03 PM
#5
Thread Starter
Frenzied Member
Re: Understanding XOR
 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
-
Nov 28th, 2007, 06:10 PM
#6
Thread Starter
Frenzied Member
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
-
Nov 28th, 2007, 06:34 PM
#7
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
-
Nov 28th, 2007, 06:41 PM
#8
Thread Starter
Frenzied Member
I'll Be Back!
T-1000
Microsoft .Net 2005
Microsoft Visual Basic 6
Prefer using API
-
Nov 28th, 2007, 06:43 PM
#9
Thread Starter
Frenzied Member
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
-
Nov 28th, 2007, 06:51 PM
#10
Re: [RESOLVED] Understanding XOR
 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."
-
Nov 28th, 2007, 06:59 PM
#11
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.
-
Nov 28th, 2007, 07:03 PM
#12
Thread Starter
Frenzied Member
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
-
Nov 28th, 2007, 07:04 PM
#13
Re: [RESOLVED] Understanding XOR
&H tells the compiler the following values are hex numbers.
-
Nov 28th, 2007, 07:18 PM
#14
Thread Starter
Frenzied Member
Re: [RESOLVED] Understanding XOR
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|