Return Value Array.BinarySearch ()

I have the following array:

double[] list = new double[] {0,0,100,100} 

Why, if I search for 29.6 , I get -3 ?

 Array.BinarySearch(list, 29.6) 

I was expecting +1 or -1 .

The Array.BinarySearch () documentation for the return parameter says:

The index of the specified value in the specified array, if the value is found. If the value is not found, and the value is less than one or more elements in the array, this is a negative number, which is a bitwise addition to the index of the first element, which is greater than the value. If the value is not found and the value is greater than any of the elements in the array, this is a negative number, which is a bitwise complement (index of the last element plus 1).

But that doesn't tell me too much.

+7
source share
3 answers

If the value is not found and the value is less than one or more elements in the array, this is a negative number, which is a bitwise addition to the index of the first element, which is greater than the value.

The first element, which is greater than 29.6, is 100 , which has an index of 2 .

~2 - -3 .

+10
source

You can use '~' to get a bitwise complement that will give you the index of the first element larger than the search element.

If the array does not contain the specified value, the method returns a negative integer. You can apply the bitwise padding operator (~) to a negative result (in Visual Basic, Xor a negative result with -1) to create an index. If this index is greater than or equal to the size of the array, the element has more elements than the value in the array. Otherwise, the index of the first element is greater than the value.

From MSDN

Thus, if you had:

 var pos = Array.BinarySearch(list, 29.6); 

You can check:

 if (pos < 0) { Console.WriteLine("Not found, the result was {0} which is index {1}", pos, ~pos); } 

Which in your case means that your -3 will indicate that index 2 is the first element that is larger than your target goal.

+5
source

Here is your answer: "a negative number, which is a bitwise addition to the index of the first element, which is greater than the value."

So, in your case, the desired value (29.6) is less than 100, which is the 3rd element in your list of arrays, and addition 3 is -3, which is the answer you received.

Here I expanded your example and created another list of arrays (list2) with some different values, then I looked for the same value as you, 29.6, now this value is 29.6 less than 100, but more than 25, and in my list 100 position 4, and her complement is -4.

So, I get the result -4, if I searched 20 in the list of arrays, I would get the answer -3.

  double[] list = new double[] { 0, 0, 100, 100 }; double[] list2 = new double[] { 10, 15, 25, 100 }; int result = Array.BinarySearch(list, 29.6); int result2 = Array.BinarySearch(list2, 29.6); Response.Write("Your answer result:" + result.ToString() + "<br/>"); Response.Write("Your answer result2:" + result2.ToString()); 

The result of my code:

  Your answer result : -3 Your answer result2: -4 

Hope this helps.

0
source

All Articles