public static int binarySearchInts(long addr, int count, int key) {
        int from = 0;
        int to = count;

        --to;

        while (from <= to) {
            int mid = from + to >>> 1;
            int midVal = MemoryUtil.getInt(addr + (mid << 2));
            if (midVal < key) {
                from = mid + 1;
            } else if (midVal > key) {
                to = mid - 1;
            } else {
                return mid;
            }
        }

        return -(from + 1);
    }
Those methods return index of the search key, if it is contained in the array
within the specified range;
otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>.  The
<i>insertion point</i> is defined as the point at which the
key would be inserted into the array: the index of the first
element in the range greater than the key,
or <tt>toIndex</tt> if all
elements in the range are less than the specified key.  Note
that this guarantees that the return value will be &gt;= 0 if
and only if the key is found.
indexr-common/src/main/java/io/indexr/util/BinarySearch.java