Help with XOR

  • Thread starter Thread starter Guest
  • Start date Start date
Chris said:
Hi,
Can someone explain the use of XOR?

Thanks

I'm not sure what exactly you mean but one interesting use is in (very
simple) encryption (and decryption with the same key).

int secret = number ^ code; // encrypt word (or number) with a code
int notSecret = secret ^ code; // now notSecret has the same value as
the original number

It also describes the operation of a two-way light switch - you know the
type where you have a long room with one light and two switches (a
switch at either end). Either can turn on the light and then either can
turn it off. (Draw a picture if you don't get it).

I guess you are aware of the definition (from online help):
Binary ^ operators are predefined for the integral types and bool. For
integral types, ^ computes the bitwise exclusive-OR of its operands. For
bool operands, ^ computes the logical exclusive-or of its operands; that
is, the result is true if and only if exactly one of its operands is true.



Eddie
 
XOR is the binary operator that performs an excluse or between the operands.

This is how excluse or works:

0 xor 0 = 0
0 xor 1 = 1
1 xor 0 = 1
1 xor 1 = 0

For operands with multiple bits, each bit is calculated against the
corresponding bit in the other operator:

0x00000000 xor 0x00001111 = 0x00001111
0x00001111 xor 0x01010101 = 0x01011010
0x11110000 xor 0x00110011 = 0x11000011
 
if you XOR mask with target
for each bit that is not set in the mask the corresponding bit in the target
is unchanged
for each bit that is set in the mask the corresponding bit in the target is
flipped

eg
01001101 XOR 00001010 = 01000111
 
What's the mask?

guy said:
if you XOR mask with target
for each bit that is not set in the mask the corresponding bit in the target
is unchanged
for each bit that is set in the mask the corresponding bit in the target is
flipped

eg
01001101 XOR 00001010 = 01000111
 
Chris said:
What's the mask?

:

The *mask* is the number that you are xor'ing against: (Per the
previous poster):

01001101 Original number
00001010 Xor (This is the mask)
--------------
01000111 Result

Notice that each bit in the result changed where the corresponding mask
bit was 1.


 
Ahhh. Got it!

Thanks

Chris Dunaway said:
The *mask* is the number that you are xor'ing against: (Per the
previous poster):

01001101 Original number
00001010 Xor (This is the mask)
--------------
01000111 Result

Notice that each bit in the result changed where the corresponding mask
bit was 1.
 
Back
Top