CS 3343/3341 Analysis of Algorithms |
Exclusive-Or, XOR,⊕
|
a | b | a∧b | a∨b | a⊕b |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 0 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
// Xor.java: test xor function ^ for interchanges public class Xor { // main function to try out Base class public static void main (String[] args) { int a = 123456789, b = -987654321; printThem(a, b, 1); // interchange a and b int temp = a; a = b; b = temp; printThem(a, b, 2); a = 345678912; b = -765432198; printThem(a, b, 1); // interchange a and b a = a^b; b = a^b; a = a^b; printThem(a, b, 2); a = 234234234; b = -789789789; printThem(a, b, 1); // interchange a and b a ^= b; b ^= a; a ^= b; printThem(a, b, 2); } // end of main private static void printThem(int a, int b, int c) { if (c == 1) System.out.print("Before, "); else System.out.print("After, "); System.out.println("a: " + a + ", \tb: " + b); if (c != 1) System.out.println(); } } Here is the output of a run: % java Xor Before, a: 123456789, b: -987654321 After, a: -987654321, b: 123456789 Before, a: 345678912, b: -765432198 After, a: -765432198, b: 345678912 Before, a: 234234234, b: -789789789 After, a: -789789789, b: 234234234 |