How are byte values obtained (XOR Example)
I have been reading this page here from MSDN regarding the XOR operator and its usage.
Half way down the page I read the following code:
// Bitwise exclusive-OR of 10 (2) and 11 (3) returns 01 (1).
Console.WriteLine("Bitwise result: {0}", Convert.ToString(0x2 ^ 0x3, 2));
Now, I cannot figure out for the life o开发者_如何学JAVAf me how 10 equates to 2, or how 11 equates to 3. Would anyone mind explaining this in simple terms so that I can clearly understand the concept here?
Thank you,
Evan
The "10" and "11" in the text are simply binary representations of numbers. So "10" in binary equals "2" in decimal, and "11" in binary equals "3" in decimal.
It's not very clear though, I admit...
(If that doesn't help, please comment saying what else is confusing. I suspect this is enough though.)
10 in binary is a 2 in decimal, 11 in binary is a 3
(10)2=1*2^1+0*2^0=2 (11)2=1*2^1+1*2^0=3
10 XOR 11 = 01
10
-
11
----
01
Exclusive means there has to be only one '1' to get a '1', in all other cases, you get a 0.
The issue here is one of base conversion. In base 2 (or binary) we represent a number a as series of zeros and ones. Take a look at http://en.wikipedia.org/wiki/Binary_numeral_system
It's showing you in binary that hexadecimal (0x2) equals 00000010 and (0x3) equals 00000011.
Therefore in XOR that is
00000010
00000011
--------
00000001
精彩评论