XOR : Java Glossary


XOR (exclusive OR) ^ is both a both Boolean and bitwise operator in Java. It gives the bitwise difference, e. g.
0b1100_0000 ^ 0b1010_0001 -> 0b0110_0001
The expression a ^ b is true if either a or b is true but not if both are true. We call this the exclusive or. In contrast the expression a | b is true if either a or b is true or both are true. We call this the inclusive or or the lawyer’s and/or.

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


if ( condition )
   toggle = ! toggle;
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  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 

// 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 decrypted 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 (Compact Disc) 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

This page is posted
on the web at:


Optional Replicator mirror
of mindprod.com
on local hard disk J:

Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

Your face IP:[]
You are visitor number