- int i = 0;
- uint8_t p_depth, msk;
-
- for (p_depth = depth; p_depth >= 8; p_depth -= 8)
- i++;
-
- msk = 1 << (7 - p_depth);
- return (ip[i] & msk) != 0;
+ uint8_t index, msk;
+
+ /*
+ * depth & 127 clamps depth to values that will not
+ * read off the end of ip.
+ * depth is the number of bits deep into ip to traverse, and
+ * is incremented in blocks of 8 (1 byte). This means the last
+ * 3 bits are irrelevant to what the index of ip should be.
+ */
+ index = (depth & INT8_MAX) / CHAR_BIT;
+
+ /*
+ * msk is the bitmask used to extract the bit used to decide the
+ * direction of the next step of the binary search.
+ */
+ msk = 1 << (7 - (depth & 7));
+
+ return (ip[index] & msk) != 0;