diff --git a/src/zutil/ByteUtil.java b/src/zutil/ByteUtil.java index aad21cf..c934eef 100755 --- a/src/zutil/ByteUtil.java +++ b/src/zutil/ByteUtil.java @@ -64,7 +64,7 @@ public class ByteUtil { * @return a new byte containing a sub byte defined by the index and length */ public static byte getBits(byte data, int length){ - return getBits(data, 0, length); + return getBits(data, length-1, length); } /** @@ -119,9 +119,9 @@ public class ByteUtil { public static byte getBitMask(int index, int length) { --length; if(0 > index || index > 7) - throw new IllegalArgumentException("Invalid index argument, allowed value is 0-7"); - if(length < 0 || 7-index-length < 0) - throw new IllegalArgumentException("Invalid length argument: "+length+", allowed values 1-8 depending on index"); + throw new IllegalArgumentException("Invalid index argument, allowed values: 0-7"); + if(length < 0 || index-length < 0) + throw new IllegalArgumentException("Invalid length argument: "+length+", allowed values: 1 to "+(index+1)+" for index "+index); return (byte) BYTE_MASK[index][length]; } } diff --git a/test/zutil/ByteUtilTest.java b/test/zutil/ByteUtilTest.java index 2000d28..2c28fad 100755 --- a/test/zutil/ByteUtilTest.java +++ b/test/zutil/ByteUtilTest.java @@ -48,6 +48,14 @@ public class ByteUtilTest { } + @Test + public void getBits(){ + assertEquals(0x01, ByteUtil.getBits((byte)0xFF, 1)); + assertEquals(0x0F, ByteUtil.getBits((byte)0xFF, 4)); + assertEquals((byte)0xFF, ByteUtil.getBits((byte)0xFF, 8)); + + } + @Test public void getBitsArray(){ assertArrayEquals(new byte[]{}, ByteUtil.getBits(new byte[]{0x00}, 0));