diff --git a/ethereumj-core/src/main/java/org/ethereum/util/ByteUtil.java b/ethereumj-core/src/main/java/org/ethereum/util/ByteUtil.java index 6f1c16e5..4f582151 100644 --- a/ethereumj-core/src/main/java/org/ethereum/util/ByteUtil.java +++ b/ethereumj-core/src/main/java/org/ethereum/util/ByteUtil.java @@ -311,9 +311,9 @@ public class ByteUtil { if ( (data.length * 8) - 1 < pos ) throw new Error("outside byte array limit, pos: " + pos); - int posByte = (pos) / 8; + int posByte = data.length - 1 - (pos) / 8; int posBit = (pos) % 8; - byte setter = (byte)(1 << (7 - posBit)); + byte setter = (byte)(1 << (posBit)); byte toBeSet = data[posByte]; byte result; if(val == 1) @@ -330,9 +330,9 @@ public class ByteUtil { if ((data.length * 8) - 1 < pos ) throw new Error("outside byte array limit, pos: " + pos); - int posByte = pos / 8; + int posByte = data.length - 1 - pos / 8; int posBit = pos % 8; byte dataByte = data[posByte]; - return Math.min(1, (dataByte & (1 << (7 - posBit)))); + return Math.min(1, (dataByte & (1 << (posBit)))); } } \ No newline at end of file diff --git a/ethereumj-core/src/test/java/test/ethereum/util/ByteUtilTest.java b/ethereumj-core/src/test/java/test/ethereum/util/ByteUtilTest.java index c3fefeec..c6226944 100644 --- a/ethereumj-core/src/test/java/test/ethereum/util/ByteUtilTest.java +++ b/ethereumj-core/src/test/java/test/ethereum/util/ByteUtilTest.java @@ -246,20 +246,20 @@ public class ByteUtilTest { */ byte[] data = ByteBuffer.allocate(4).putInt(0).array(); int posBit = 24; - int expected = 128; + int expected = 16777216; int result = -1; byte[] ret = ByteUtil.setBit(data, posBit, 1); result = ByteUtil.byteArrayToInt(ret); assertTrue(expected == result); posBit = 25; - expected = 192; + expected = 50331648; ret = ByteUtil.setBit(data, posBit, 1); result = ByteUtil.byteArrayToInt(ret); assertTrue(expected == result); posBit = 2; - expected = 536871104; + expected = 50331652; ret = ByteUtil.setBit(data, posBit, 1); result = ByteUtil.byteArrayToInt(ret); assertTrue(expected == result); @@ -268,13 +268,13 @@ public class ByteUtilTest { Set off */ posBit = 24; - expected = 536870976; + expected = 33554436; ret = ByteUtil.setBit(data, posBit, 0); result = ByteUtil.byteArrayToInt(ret); assertTrue(expected == result); posBit = 25; - expected = 536870912; + expected = 4; ret = ByteUtil.setBit(data, posBit, 0); result = ByteUtil.byteArrayToInt(ret); assertTrue(expected == result);