简体   繁体   English

使用Xor运算符时发生Java编译错误

[英]Java compilation error while using xor operator

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) 我试图用Java编写一个字符串反向函数(我知道我可以简单地调用一个现有函数,但是我正在尝试实践和学习)

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? 这里发生了什么?

    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. 您只转换第一个char ,而不转换其他char ,因此以char的integer/decimal value而不是char value本身给出xor的结果。

sample: 样品:

instead you can wrap it in parenthesis and cast the result of the manipulation to char. 相反,您可以将其包装在括号中,并将操作结果转换为char。

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

尝试转换整个表达式:

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

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

In your code actually you are trying to insert an integer value in char type variable. 实际上,在您的代码中,您试图在char类型变量中插入一个整数值。
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 ie xor operation. 之所以发生这种情况,是因为编译器将字符类型ab转换为整数类型,然后执行按位运算,即xor操作。 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;

ie in your case : 即在您的情况下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM