How java uses the XOR () operator to implement character inversion
This article is about how java uses the XOR () operator to reverse characters. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Reverse using the XOR (^) operator
Package net.javaguides.corejava.string;/*** * @ author yisu**/public class ReverseStringWithXOR {public static void main (String [] args) {ReverseStringWithXOR stringWithXOR = new ReverseStringWithXOR (); stringWithXOR.reverseWithXOR ("javaguides");} public String reverseWithXOR (String string) {final char [] array = string.toCharArray (); final int length = array.length;final int half = (int) Math.floor (array.length / 2); for (int I = 0; I < half; ionization +) {array [I] ^ = array [length-I-1] Array [length-I-1] ^ = array [I]; array [I] ^ = array [length-I-1];} display (string, String.valueOf (array)); return String.valueOf (array);} private void display (String input, String output) {System.out.println ("input string::" + input); System.out.println ("output string::" + output);}}
Output:
Input string:: javaguidesoutput string:: sediugavaj Thank you for reading! This is the end of the article on "how java uses the XOR () operator to achieve character inversion". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!