Vis*_*rey 5 java logical-operators
如何在简单的for循环中编写以下代码:
int asInt = (valueAsBytes[3] & 0xFF)
| ((valueAsBytes[2] & 0xFF) << 8)
| ((valueAsBytes[1] & 0xFF) << 16)
| ((valueAsBytes[0] & 0xFF) << 24);
Run Code Online (Sandbox Code Playgroud)
请注意,每次访问时数组索引减1 valueAsBytes,而shift运算符的第二个操作数增加8:
int asInt = 0;
for (int i = valueAsBytes.length-1; i >= 0; i--)
asInt |= valueAsBytes[i] & 0xFF << (valueAsBytes.length-i)*8;
Run Code Online (Sandbox Code Playgroud)