I am confused about the BinarySearch method of List<T>
in case when the item does not exist.
I've got
List<long> theList = {1, 3, 5, ...}.
theList.BInarySearch(0)
returns 0, and theList.BInarySearch(3)
returns 1, as expected.
However, theList.BinarySearch(1)
returns -2, and not -1 as I'd expect. The MSDN manual says:
"Return value: The zero-based index of item in the sorted List, if item is found; otherwise, a negative number that is the bitwise complement of the index of the next element that is larger than item or, if there is no larger element, the bitwise complement of Count."
A "bitwise complement"? What Am I missing here and why is it that theList.BinarySearch(1) != -1
?
theList.BinarySearch(2)
?1
is right there... – Keavy