XOR : Java Glossary
home X words local find no local find frame, full screen Google search web for topic jump to footer translate with Babelfish 2007-08-09 by Roedy Green ©1996-2008 Canadian Mind Products
Go to : punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
XOR
^ is both a both boolean and bitwise operator in Java. It gives the bitwise difference. e.g.
0b1100_0000 ^ 0b1010_0001 -> 0b0110_0001
XOR has almost magical properties:
Conditional Toggle Swap Without a Temporary
Error Correcting Codes Calculating Aggregate HashCodes with XOR
Disguising Passwords embedded in Java Computer Source Links
One-Time Pad Uncrackable Encryption

Conditional Toggle

Consider a conditional toggle like this:
if ( condition )
   {
   toggle = ! toggle;
   }
You can code it more tersely like this:
toggle ^= condition;
Further, the generated byte code for the xor version will be more compact, and the generated machine code will execute more quickly.

Error Correcting Codes

Here is the essence of a scheme I cooked up for Peter Norton used in his Norton Floppy Backup. The idea was if a diskette was scratched destroying a circular track, or scratched radially, or if a big buttery thumbprint destroyed a blob of the disk, it was still recoverable because the data were recorded redundantly. For every 10 sectors of data recorded, there was one extra sector, the xor of those ten sectors. That extra sector was computed like this:
If a sector were destroyed, you would XOR together the remaining 10 sectors to reconstruct the missing sector. The trick was placing the sectors so that damage would unlikely hit two sectors in the same group. If that happened, you were hosed.

Disguising Passwords embedded in Java Computer Source

One-Time Pad Uncrackable Encryption

If a[] is a byte array of data, and k[] is an equally long byte a array of truly random gibberish, not just pseudorandom, then you can encrypt the data with xor this way:
// encrypting  a[] with key k[] into e[]
// a[] = array to be encrypted
// k[] = key of truly random gibberish
// e[] = encrypted data
byte[] e = new byte[a.length];
for ( int i=0; i<a.length; i++ )
   {
   e[i] = (byte)( a[i] ^ k[i] );
   }
Someone peeking at e[] who has no copy of k[] has no way on earth of guessing what a[] is. Every possible encryption of every possible message is equally probable. The decrytped message could equally well say, "plan B tonight at seven" or "The Snow Queen---------". You could get either result just using two different decrypting keys. Who is to say which is the real one? To decrypt, you use xor again with the same k[]:
// decrypting e[] with key k[] into a[]
// e[] = array to be decrypted
// k[] = key of truly random gibberish
// a[] = original data
byte[] a = new byte[e.length];
for ( int i=0; i<e.length; i++ )
   {
   a[i] = (byte)( e[i] ^ k[i] );
   }
For this to be secure, you must never reuse a key k. A pre-computer era version of this was known as the Vernam cipher. It is used for diplomatic messages that absolutely must not be cracked. The main problem with it is you somehow have to get the keys to the other end without them being viewed. You can do that with CD and secure courier. If you suspect compromise, you don’t use the keys. You need software to make sure you never reuse a key.

Swap Without a Temporary

In Java’s RAM rich environments this trick is only a curiosity.

CMP_homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.16] The information on this page is for non-military use only.
You are visitor number 88,464. Military use includes use by defence contractors.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/Mindprod website mirror)
http://mindprod.com/jgloss/xor.html J:\mindprod\jgloss\xor.html