Reputation: 309
I have the following piece of Java code and while debugging in Eclipse, Windows 7, the variable 'xoredChar' shows no value at all, not null, not '', nothing.
char xoredChar = (char) (stringA.charAt(j)^stringB.charAt(j));
Why is that? I need to understand how can I do this xor operation between two characters in java. What am I missing?
Upvotes: 1
Views: 16956
Reputation: 8068
As mentioned by the other answers, xoring the same characters results in a \0
value, which has no visual representation. Perhapse you are interested in a small application, which gives you and idea how XOR works on your strings:
public class Example {
public static void main(String[] args) {
String a = "abcde";
String b = a;
for (int idx = 0; idx < b.length(); idx++) {
System.out.printf("xoring <%s> [%s] with <%s> [%s]\n",
a.charAt(0), toBinaryString(a.charAt(0)),
b.charAt(idx), toBinaryString(b.charAt(idx)));
int c = (a.charAt(0) ^ b.charAt(idx));
System.out.printf("result is <%s> [%s]\n",
(char) c, toBinaryString(c));
}
}
}
Have fun!
Upvotes: 0
Reputation: 201457
Well, if the strings are equal you'll get back a \0
which is not a printable character. Try something like this,
String stringA = "A";
String stringB = "A";
int j = 0;
char xoredChar = (char) (stringA.charAt(j) ^ stringB.charAt(j));
System.out.printf("'%c' = %d\n", xoredChar, (int) xoredChar);
Output is
' ' = 0
Upvotes: 3
Reputation: 234715
If stringA
and stringB
are identical, then the XOR operation will yield xoredChar = 0
.
A 0 is probably showing in your IDE as nothing since 0 is used as a string terminator in most instances.
Upvotes: 0