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

后端 未结 5 1898
情话喂你
情话喂你 2020-12-07 02:10
short permissions = 0755;
short requested = 0700;
short result = permissions & requested; 

I get a compiler error:

error possib         


        
5条回答
  •  没有蜡笔的小新
    2020-12-07 03:12

    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. Then:
    • If either operand 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 type int.

提交回复
热议问题