为什么两个字节的xor运算符产生一个int? [英] Why does the xor operator on two bytes produce an int?

查看:233
本文介绍了为什么两个字节的xor运算符产生一个int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

        //key & hash are both byte[]
        int leftPos = 0, rightPos = 31;
        while(leftPos < 16) {
            //possible loss of precision. required: byte, found: int
            key[leftPos] = hash[leftPos] ^ hash[rightPos];
            leftPos++;
            rightPos--;
        }

为什么对Java中的两个字节进行逐位操作会返回int?我知道我可以把它回到字节,但它似乎愚蠢。

Why would a bitwise operation on two bytes in Java return an int? I know I could just cast it back to byte, but it seems silly.

推荐答案

因为语言规范说。它没有原因,但我怀疑这些是最可能的意图:

Because the language spec says so. It gives no reason, but I suspect that these are the most likely intentions:


  • 有一个小而简单的规则来覆盖算术操作涉及所有可能的类型组合

  • 为了有效实现 - 32位整数是CPU在内部使用的,其他一切都需要转换,显式或隐式。

这篇关于为什么两个字节的xor运算符产生一个int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆