Why does the 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 completely mistaken, the result of the AND binary as long as it is a long operand. Why is the result an integer?

Would it be a performance hit if I threw a short one?

 (short) permissions & requested 
+4
source share
5 answers

If I'm not completely mistaken, the result is binary AND until the longest operand. Why is the result an integer?

Since the Java language specification says that the result of a short integer arithmetic is always int. This was probably written in support of the fact that 32-bit processors work the same way as internally - in fact, they have no way to do arithmetic with shorts.

Would it be a performance hit if I threw a short one?

For the reason mentioned above: no - this should happen anyway.

+3
source

The short answer (hah!) Is binary digital advertising .

  • If any of the operands has a reference type, the decompression conversion is performed (Section 5.1.8). Then:
  • If one of the operands is of type double, the other is converted to double.
  • Otherwise, if either operand is of type float, the other is converted to float.
  • Otherwise, if either operand is of type long, the other is converted to long.
  • Otherwise, both operands are converted to int type.
+6
source

I just wanted to add that you can actually avoid translation if you use arithmetic assignment operators. It is not faster or slower, just something that may be nice to know.

 short permissions = 0755; short requested = 0700; short result = permissions; result &= requested; 
+2
source

In fact, I suspect that you might make a performance hit. There are only Java byte codes for bitwise operations on int and long values. Therefore, short values ​​in the permission and requested variables need (theoretically) extension of the character before the operation is performed.

(And besides, I think you will find that native bit instructions are only available in 32 and 64 bits. Or, if there are 8 or 16 bit versions, they will take as many hours as the 32 bit version. CPU datapaths will have at least 32 bits wide, and for and / or / xor it is impossible to speed up the work of narrower types.)

In addition, although the three variables are of type short , the JVM will allocate the same number of bytes to store them. This is a consequence of how the JVM is designed.

So, if your goal of using short was to save space or time, this probably won't help. But the only way to make sure you use the profiler to compare the short and int versions of your application ... or better yet, just forget about it.

+2
source

The operands of the & operator will be passed to int, and so the result will be an int, which should be discarded to a short one if you want to store it in result . I do not think that this should be a penalty for performance if the compiler does not work when generating the code.

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

When both operands of the operator &, or, or | are of a type that is convertible (§ 5.1.8), to a primitive integral type, binary numeric promotion is first performed on operands (§5.6.2).

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

[...] Otherwise, both operands are converted to type int.

+1
source

Source: https://habr.com/ru/post/1301557/


All Articles