XOR : Java Glossary

xor
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

Consider
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 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 (Compact Disk) 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

CMP homejump to top You can get the freshest copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/xor.html J:\mindprod\jgloss\xor.html
logofeedback Please email your feedback for publication, letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email If you want your message kept confidential, not considered for posting, please explicitly specify that.
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.179.210]
You are visitor number 132,514.