1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | class test { public static int toInt(byte[] bytes, int offset) { int ret = 0; for (int i=0; i<4 && i+offset<bytes.length; i++) { ret <<= 8; ret |= (int)bytes[i] & 0xFF; } return ret; } public static int toIntFixed(byte[] bytes, int offset) { int ret = 0, max = offset + 4; for (int i = offset; i < max && i < bytes.length; ++i) { ret <<= 8; ret |= (int)bytes[i] & 0xFF; } return ret; } public static void main(String[] args) { System.out.println("The original version, note that the output should be the same:"); byte[] bytes = new byte[]{-2, -4, -8, -16}; System.out.println(Integer.toBinaryString(toInt(bytes, 0))); // 11111110111111001111100011110000 as expected bytes = new byte[]{-1, -2, -4, -8, -16}; System.out.println(Integer.toBinaryString(toInt(bytes, 1))); // Expected: 11111110111111001111100011110000, should remain the same // Got: 11111111111111101111110011111000 System.out.println(); System.out.println("The fixed versoin:"); bytes = new byte[]{-2, -4, -8, -16}; System.out.println(Integer.toBinaryString(toIntFixed(bytes, 0))); bytes = new byte[]{-1, -2, -4, -8, -16}; System.out.println(Integer.toBinaryString(toIntFixed(bytes, 1))); } } |
Y2xhc3MgdGVzdCB7CiAgICBwdWJsaWMgc3RhdGljIGludCB0b0ludChieXRlW10gYnl0ZXMsIGludCBvZmZzZXQpIHsKICAgICAgICBpbnQgcmV0ID0gMDsKICAgICAgICBmb3IgKGludCBpPTA7IGk8NCAmJiBpK29mZnNldDxieXRlcy5sZW5ndGg7IGkrKykgewogICAgICAgICAgICByZXQgPDw9IDg7CiAgICAgICAgICAgIHJldCB8PSAoaW50KWJ5dGVzW2ldICYgMHhGRjsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHJldDsKICAgIH0KICAgIAogICAgcHVibGljIHN0YXRpYyBpbnQgdG9JbnRGaXhlZChieXRlW10gYnl0ZXMsIGludCBvZmZzZXQpIHsKICAgICAgICBpbnQgcmV0ID0gMCwgbWF4ID0gb2Zmc2V0ICsgNDsKICAgICAgICBmb3IgKGludCBpID0gb2Zmc2V0OyBpIDwgbWF4ICYmIGkgPCBieXRlcy5sZW5ndGg7ICsraSkgewogICAgICAgICAgICByZXQgPDw9IDg7CiAgICAgICAgICAgIHJldCB8PSAoaW50KWJ5dGVzW2ldICYgMHhGRjsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHJldDsKICAgIH0KCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJUaGUgb3JpZ2luYWwgdmVyc2lvbiwgbm90ZSB0aGF0IHRoZSBvdXRwdXQgc2hvdWxkIGJlIHRoZSBzYW1lOiIpOwogICAgICAgIGJ5dGVbXSBieXRlcyA9IG5ldyBieXRlW117LTIsIC00LCAtOCwgLTE2fTsKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oSW50ZWdlci50b0JpbmFyeVN0cmluZyh0b0ludChieXRlcywgMCkpKTsKICAgICAgICAvLyAxMTExMTExMDExMTExMTAwMTExMTEwMDAxMTExMDAwMCBhcyBleHBlY3RlZAogICAgICAgIAogICAgICAgIGJ5dGVzID0gbmV3IGJ5dGVbXXstMSwgLTIsIC00LCAtOCwgLTE2fTsKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oSW50ZWdlci50b0JpbmFyeVN0cmluZyh0b0ludChieXRlcywgMSkpKTsKICAgICAgICAvLyBFeHBlY3RlZDogMTExMTExMTAxMTExMTEwMDExMTExMDAwMTExMTAwMDAsIHNob3VsZCByZW1haW4gdGhlIHNhbWUKICAgICAgICAvLyBHb3Q6ICAgICAgMTExMTExMTExMTExMTExMDExMTExMTAwMTExMTEwMDAKICAgICAgICAKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oKTsKICAgICAgICAKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIlRoZSBmaXhlZCB2ZXJzb2luOiIpOwogICAgICAgIGJ5dGVzID0gbmV3IGJ5dGVbXXstMiwgLTQsIC04LCAtMTZ9OwogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbihJbnRlZ2VyLnRvQmluYXJ5U3RyaW5nKHRvSW50Rml4ZWQoYnl0ZXMsIDApKSk7CiAgICAgICAgCiAgICAgICAgYnl0ZXMgPSBuZXcgYnl0ZVtdey0xLCAtMiwgLTQsIC04LCAtMTZ9OwogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbihJbnRlZ2VyLnRvQmluYXJ5U3RyaW5nKHRvSW50Rml4ZWQoYnl0ZXMsIDEpKSk7CiAgICB9Cn0K
-
upload with new input
-
result: Success time: 0.02s memory: 245632 kB returned value: 0
The original version, note that the output should be the same: 11111110111111001111100011110000 11111111111111101111110011111000 The fixed versoin: 11111110111111001111100011110000 11111110111111001111100011110000


