• 0

[JAVA] XOR function


Question

9 answers to this question

Recommended Posts

  • 0

You need to convert them to integers first. You could do it the hard way and code a function to do that yourself, or you can use the built-in Integer class:

int bin1 = Integer.valueOf("10010101", 2), bin2 = Integer.valueOf("00101001", 2);
int result = bin1 ^ bin2;

Details on the valueOf() method that I used can be at http://java.sun.com/javase/6/docs/api/java....String,%20int)

Note that it returns an instance of the Integer class. However, this can be coerced to a native int type, so you shouldn't have problems with the above code. If you do (my Java is a bit rusty), just call the intValue() method on the result like this:

int bin1 = Integer.valueOf("10010101", 2).intValue(), bin2 = Integer.valueOf("00101001", 2).intValue();
int result = bin1 ^ bin2;

That's probably what happens internally anyway, but for code clarity, I omitted it because it shouldn't be necessary.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.