Skip to content

Commit 4c2bc17

Browse files
committed
fix bug in bitcount helper, fixes graphhopper#809
1 parent 9da912f commit 4c2bc17

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

core/src/main/java/com/graphhopper/util/Helper.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,15 @@ static String packageToPath(Package pkg) {
8888
}
8989

9090
public static int countBitValue(int maxTurnCosts) {
91-
double val = Math.log(maxTurnCosts) / Math.log(2);
92-
int intVal = (int) val;
93-
if (val == intVal)
94-
return intVal;
95-
return intVal + 1;
91+
if (maxTurnCosts < 0)
92+
throw new IllegalArgumentException("maxTurnCosts cannot be negative " + maxTurnCosts);
93+
94+
int counter = 0;
95+
while (maxTurnCosts > 0) {
96+
maxTurnCosts >>= 1;
97+
counter++;
98+
}
99+
return counter++;
96100
}
97101

98102
public static void loadProperties(Map<String, String> map, Reader tmpReader) throws IOException {

core/src/test/java/com/graphhopper/util/HelperTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ public void tearDown() {
4747

4848
@Test
4949
public void testCountBitValue() throws Exception {
50-
assertEquals(2, Helper.countBitValue(4));
50+
assertEquals(1, Helper.countBitValue(1));
51+
assertEquals(2, Helper.countBitValue(2));
52+
assertEquals(2, Helper.countBitValue(3));
53+
assertEquals(3, Helper.countBitValue(4));
54+
assertEquals(3, Helper.countBitValue(7));
55+
assertEquals(4, Helper.countBitValue(8));
5156
assertEquals(5, Helper.countBitValue(20));
5257
}
5358

0 commit comments

Comments
 (0)