fixing bloom filter - counting from the right not from the left !!!

This commit is contained in:
romanman 2014-12-03 22:19:53 +01:00
parent 282657f41d
commit 18eeb587a9
2 changed files with 9 additions and 9 deletions

View File

@ -311,9 +311,9 @@ public class ByteUtil {
if ( (data.length * 8) - 1 < pos ) if ( (data.length * 8) - 1 < pos )
throw new Error("outside byte array limit, pos: " + 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; int posBit = (pos) % 8;
byte setter = (byte)(1 << (7 - posBit)); byte setter = (byte)(1 << (posBit));
byte toBeSet = data[posByte]; byte toBeSet = data[posByte];
byte result; byte result;
if(val == 1) if(val == 1)
@ -330,9 +330,9 @@ public class ByteUtil {
if ((data.length * 8) - 1 < pos ) if ((data.length * 8) - 1 < pos )
throw new Error("outside byte array limit, pos: " + 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; int posBit = pos % 8;
byte dataByte = data[posByte]; byte dataByte = data[posByte];
return Math.min(1, (dataByte & (1 << (7 - posBit)))); return Math.min(1, (dataByte & (1 << (posBit))));
} }
} }

View File

@ -246,20 +246,20 @@ public class ByteUtilTest {
*/ */
byte[] data = ByteBuffer.allocate(4).putInt(0).array(); byte[] data = ByteBuffer.allocate(4).putInt(0).array();
int posBit = 24; int posBit = 24;
int expected = 128; int expected = 16777216;
int result = -1; int result = -1;
byte[] ret = ByteUtil.setBit(data, posBit, 1); byte[] ret = ByteUtil.setBit(data, posBit, 1);
result = ByteUtil.byteArrayToInt(ret); result = ByteUtil.byteArrayToInt(ret);
assertTrue(expected == result); assertTrue(expected == result);
posBit = 25; posBit = 25;
expected = 192; expected = 50331648;
ret = ByteUtil.setBit(data, posBit, 1); ret = ByteUtil.setBit(data, posBit, 1);
result = ByteUtil.byteArrayToInt(ret); result = ByteUtil.byteArrayToInt(ret);
assertTrue(expected == result); assertTrue(expected == result);
posBit = 2; posBit = 2;
expected = 536871104; expected = 50331652;
ret = ByteUtil.setBit(data, posBit, 1); ret = ByteUtil.setBit(data, posBit, 1);
result = ByteUtil.byteArrayToInt(ret); result = ByteUtil.byteArrayToInt(ret);
assertTrue(expected == result); assertTrue(expected == result);
@ -268,13 +268,13 @@ public class ByteUtilTest {
Set off Set off
*/ */
posBit = 24; posBit = 24;
expected = 536870976; expected = 33554436;
ret = ByteUtil.setBit(data, posBit, 0); ret = ByteUtil.setBit(data, posBit, 0);
result = ByteUtil.byteArrayToInt(ret); result = ByteUtil.byteArrayToInt(ret);
assertTrue(expected == result); assertTrue(expected == result);
posBit = 25; posBit = 25;
expected = 536870912; expected = 4;
ret = ByteUtil.setBit(data, posBit, 0); ret = ByteUtil.setBit(data, posBit, 0);
result = ByteUtil.byteArrayToInt(ret); result = ByteUtil.byteArrayToInt(ret);
assertTrue(expected == result); assertTrue(expected == result);