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

查看:38
本文介绍了为什么两个字节上的异或运算符会产生一个 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 内部使用的,其他一切都需要显式或隐式转换.

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

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