为什么按位两个短值,并导致Java的一个int值? [英] Why does bitwise AND of two short values result in an int value in Java?

查看:136
本文介绍了为什么按位两个短值,并导致Java的一个int值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 短权限= 0755;
短请求= 0700;
总之结果=权限和放大器;要求;

我得到一个编译错误:

  precision的错误可能发生的损失
发现:INT
要求:短

如果我不是二进制完全错误的结果,是只要最长的操作数。为什么结果的整数?

会不会有性能损失,如果我会投短?

 (短)的权限和放大器;要求


解决方案

  

如果我不是完全错误的结果
  二进制AND是只要最长
  操作数。为什么结果的整数?


由于Java语言规范中说,非长整数运算的结果总是一个int。它可能是写在一个事实,即32位CPU的工作就像是承认的方式在内部反正 - 他们居然没有的有无的办法做算术搭配短裤


  

会不会有性能损失,如果我会投短?


有关上面给出的理由是:没有 - 这将反正要发生

short permissions = 0755;
short requested = 0700;
short result = permissions & requested; 

I get a compiler error:

error possible loss of precision
found   : int
required: short

If I'm not totally wrong the result of binary AND is as long as the longest operand. Why is the result an integer?

Would there be a performance hit, if I would cast to short?

(short) permissions & requested

解决方案

If I'm not totally wrong the result of binary AND is as long as the longest operand. Why is the result an integer?

Because the Java Language Specification says that the result of non-long integer arithmetic is always an int. It was probably written that way in acknowledgment of the fact that 32 bit CPUs work like that internally anyway - they actually don't have a way to do arithmetic with shorts.

Would there be a performance hit, if I would cast to short?

For the reason given above: no - it will have to happen anyway.

这篇关于为什么按位两个短值,并导致Java的一个int值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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