The bitwise XOR expression is used to compute the bit-by-bit Boolean XOR (exclusive OR) of two integer values.
The bitwise XOR expression is typically used to toggle the bits of an integer used as a bit field.
| Boolean XOR | ||
|---|---|---|
| Operands | Value | |
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
class BitwiseXor {
String Bits (int Value, int Width) {
String P;
String S;
if (Value < 0) {
if (Width < 32) {
S = "**********************************";
S = S.substring (0, Width);
} else {
Value = Integer.MAX_VALUE & Value;
P = "0000000000000000000000000000000";
S = Integer.toString (Value, 2);
S = "1" + P.substring (1, 32 - S.length()) + S;
if (Width > 32) {
P = " ";
S = P.substring (0, Width - 32) + S;
}
}
} else {
P = " ";
S = Integer.toString (Value, 2);
S = P.substring (0, Width - S.length()) + S;
if (S.length () > Width) {
S = "**********************************";
S = S.substring (0, Width);
}
}
return S;
}
String Decimal (int Value, int Width) {
String P = " ";
String S = Integer.toString (Value);;
if (S.length () > Width) {
S = "**********************************";
S = S.substring (0, Width);
} else if (S.length () < Width) {
S = P.substring (0, Width - S.length()) + S;
}
return S;
}
void DisplayXOR (int A, int B) {
int C;
C = A ^ B;
System.out.println ( " "
+ Decimal (A, 11) + " " + Bits (A, 32));
System.out.println ( "^ "
+ Decimal (B, 11) + " " + Bits (B, 32));
System.out.println ( " ----------- --------------------------------");
System.out.println ( "= "
+ Decimal (C, 11) + " " + Bits (C, 32));
System.out.println ( " ");
}
public static void main (String[] Args) {
BitwiseXor App;
App = new BitwiseXor();
App.DisplayXOR (5, 3);
App.DisplayXOR (-5, -3);
App.DisplayXOR (Integer.MAX_VALUE, Integer.MIN_VALUE );
}
}
| Bitwise XOR Operator | <bitwise-xor-operator> |
| Bitwise AND Expression | <bitwise-and-expression> |