简体   繁体   English

为什么两个短值的按位与会导致 Java 中的 int 值?

[英]Why does bitwise AND of two short values result in an int value in Java?

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.如果我没有完全错,二进制 AND 的结果与最长的操作数一样长。 Why is the result an integer?为什么结果是整数?

Would there be a performance hit, if I would cast to short?如果我选择做空,会不会影响性能?

(short) permissions & requested

The short answer (hah!) is, binary numeric promotion .简短的回答(哈!)是, 二进制数字提升

  • If any of the operands is of a reference type, unboxing conversion (§5.1.8) is performed.如果任何操作数是引用类型,则执行拆箱转换(第 5.1.8 节)。 Then:然后:
  • If either operand is of type double, the other is converted to double.如果任一操作数的类型为 double,则另一个将转换为 double。
  • Otherwise, if either operand is of type float, the other is converted to float.否则,如果任一操作数的类型为 float,则另一个将转换为 float。
  • Otherwise, if either operand is of type long, the other is converted to long.否则,如果任一操作数的类型为 long,则另一个将转换为 long。
  • Otherwise, both operands are converted to type int.否则,两个操作数都被转换为 int 类型。

If I'm not totally wrong the result of binary AND is as long as the longest operand.如果我没有完全错,二进制 AND 的结果与最长的操作数一样长。 Why is the result an integer?为什么结果是整数?

Because the Java Language Specification says that the result of non-long integer arithmetic is always an int.因为 Java 语言规范说非长整数运算的结果总是一个 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.它可能写在的事实,32位CPU一样,内部的工作无论如何确认的方式-他们居然没有办法做算术搭配短裤。

Would there be a performance hit, if I would cast to short?如果我选择做空,会不会影响性能?

For the reason given above: no - it will have to happen anyway.由于上述原因:不-无论如何都必须发生。

I just wanted to add that you can actually avoid the cast if you use the arithmetic assignment operators.我只想补充一点,如果您使用算术赋值运算符,您实际上可以避免强制转换。 It's not faster or slower, just something that might be nice to know.它不是更快或更慢,只是一些可能很高兴知道的东西。

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

Actually, I suspect that you might take a performance hit.实际上,我怀疑您的性能可能会受到影响。 There are only Java bytecodes for bitwise operations on int and long values.对于intlong值的按位运算只有 Java 字节码。 So the short values in permission and requested variables need (in theory) to be sign-extended before the operation is performed.因此,在执行操作之前,需要(理论上)对permissionrequested变量中的short值进行符号扩展。

(And besides, I think you will find that the native bit-wise instructions are only available in 32 and 64 bit. Or if there are 8 or 16 bit versions, they will take the same number of clocks as the 32 bit version. The CPU datapaths will be at least 32 bits wide, and for and/or/xor there is no way to make narrower types work faster.) (此外,我认为您会发现原生按位指令仅适用于 32 位和 64 位。或者如果有 8 位或 16 位版本,它们将采用与 32 位版本相同数量的时钟。 CPU 数据路径将至少为 32 位宽,并且 for and/or/xor 没有办法使更窄的类型工作得更快。)

In addition, even though the three variables have type short , the JVM will allocate the same number of bytes to store them.此外,即使这三个变量的类型为short ,JVM 也会分配相同数量的字节来存储它们。 This is a consequence of the way that the JVM is designed.这是 JVM 设计方式的结果。

So if your aim in using short was to save space or time, it probably won't help.因此,如果您使用short的目的是节省空间或时间,那么它可能无济于事。 But the only way to be sure is to use a profiler to compare the short and int versions of your application ... or better still, just forget about it.但唯一可以确定的方法是使用分析器来比较应用程序的shortint版本......或者更好的是,忘记它。

The operands to the & operator will be promoted to int, and therefore the result is int, which will have to be casted to short if you want to store it in result . &运算符的操作数将被提升为 int,因此结果是 int,如果要将其存储在result ,则必须将其强制转换为 short。 I don't think it should be a performance penalty unless the compiler is bad at generating code.我不认为这应该是性能损失,除非编译器不擅长生成代码。

From http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.22.1 :http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.22.1

When both operands of an operator &, ^, or |当运算符 &、^ 或 | 的两个操作数are of a type that is convertible (§5.1.8) to a primitive integral type, binary numeric promotion is first performed on the operands (§5.6.2).是可转换(第 5.1.8 节)为原始整数类型的类型,首先对操作数执行二进制数字提升(第 5.6.2 节)。

From http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#170983 :http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#170983

[...] Otherwise, both operands are converted to type int. [...] 否则,两个操作数都被转换为 int 类型。

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

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