Arrays.binarySearch does not work for arrays larger than 1<<30

XMLWordPrintable

    • Type: Bug
    • Resolution: Duplicate
    • Priority: P3
    • None
    • Affects Version/s: 6
    • Component/s: core-libs
    • generic
    • generic

      Various binary search methods in java.util.Arrays compute the average of
      two positive indices (ints) like this:

      int mid = (low + high) >> 1;

      but if low + high overflows into negative territory, the result is negative,
      probably giving undefined results. An easy fix is the simple


      int mid = (low + high) >>> 1;

      although I have not tested whether this is enough to get sort + binary search
      to actually work properly on such gigantic arrays.

            Assignee:
            Martin Buchholz
            Reporter:
            Martin Buchholz
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: