0

I am attempting to write a string reverse function in Java (I am aware I can simply call an existing function, but I am trying this to practice and learn)

public class HelloWorld{

    public static void main(String []args){
        String s = reverse("abcd");
        System.out.println(s);

    }
    public static String reverse(String str){
        int end = str.length() - 1;
        int start = 0;

        char[] arr = str.toCharArray();
        while (start < end)
        {
            arr[start] =  arr[start] ^ arr[end];

            arr[end] = arr[start] ^ arr[end];

            arr[start] = arr[start] ^ arr[end];

            start++;
            end--;
        }
        String ret = new String(arr);
        return ret; 
    }
}

However, it gives me these errors:

HelloWorld.java:16: error: possible loss of precision
            arr[start] =  arr[start] ^ arr[end];
                                     ^
  required: char
  found:    int
HelloWorld.java:18: error: possible loss of precision
            arr[end] = arr[start] ^ arr[end];
                                  ^
  required: char
  found:    int
HelloWorld.java:20: error: possible loss of precision
            arr[start] = arr[start] ^ arr[end];
                                    ^
  required: char
  found:    int
3 errors

I have tried casting like

arr[end] = (char) arr[start] ^ arr[end];

Didn't solve the problem. What is going on here?

periphery
  • 75
  • 1
  • 9
  • 1
    Your problem is much alike http://stackoverflow.com/questions/2279718/why-does-bitwise-and-of-two-short-values-result-in-an-int-value-in-java – ljgw Aug 08 '14 at 05:36

4 Answers4

7
    I have tried casting like
    arr[end] = (char) arr[start] ^ arr[end];

You are only casting the first char but not the other one thus giving you the result of xor in integer/decimal value of char not the char value itself.

sample:

instead you can wrap it in parenthesis and cast the result of the manipulation to char.

(char) (arr[start] ^ arr[end]);
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
1

Try to cast the whole expression:

arr[end] = (char) (arr[start] ^ arr[end]);
Jens
  • 67,715
  • 15
  • 98
  • 113
1

This is working fine :

public class HelloWorld {
    public static void main(String[] args) {
        String s = reverse("abcd");
        System.out.println(s);
    }

    public static String reverse(String str) {
         int end = str.length() - 1;
        int start = 0;

        char[] arr = str.toCharArray();
        while (start < end) {
            arr[start] = (char) (arr[start] ^ arr[end]);

            arr[end] = (char) (arr[start] ^ arr[end]);

            arr[start] = (char) (arr[start] ^ arr[end]);

            start++;
            end--;
        }
        String ret = new String(arr);
        return ret;
    }
}

Output :

dcba
OO7
  • 2,785
  • 1
  • 21
  • 33
1

In your code actually you are trying to insert an integer value in char type variable.
When a xor operation is performed, the implicitly cast the value into integer type.

arr[start] =  arr[start] ^ arr[end];

Consider the following:

char a = 'x';
char b = 'y';
char c = a ^ b;  //Error!

It is happening because compiler is converting character type a and b into an integer types, and then it perform the bitwise operation i.e. xor operation. the returned value will be of integer type.
To make it work properly you have to explicitly cast the return type of the operation :

char c = (char) a ^ b;

i.e. in your case :

arr[start] = (char) arr[start] ^ arr[end];
afzalex
  • 8,598
  • 2
  • 34
  • 61