public static int unsignedDifference(final short[] set1, final int length1, final short[] set2,
      final int length2, final short[] buffer) {
    int pos = 0;
    int k1 = 0, k2 = 0;
    if (0 == length2) {
      System.arraycopy(set1, 0, buffer, 0, length1);
      return length1;
    }
    if (0 == length1) {
      return 0;
    }
    short s1 = set1[k1];
    short s2 = set2[k2];
    while (true) {
      if (toIntUnsigned(s1) < toIntUnsigned(s2)) {
        buffer[pos++] = s1;
        ++k1;
        if (k1 >= length1) {
          break;
        }
        s1 = set1[k1];
      } else if (toIntUnsigned(s1) == toIntUnsigned(s2)) {
        ++k1;
        ++k2;
        if (k1 >= length1) {
          break;
        }
        if (k2 >= length2) {
          System.arraycopy(set1, k1, buffer, pos, length1 - k1);
          return pos + length1 - k1;
        }
        s1 = set1[k1];
        s2 = set2[k2];
      } else {// if (val1>val2)
        ++k2;
        if (k2 >= length2) {
          System.arraycopy(set1, k1, buffer, pos, length1 - k1);
          return pos + length1 - k1;
        }
        s2 = set2[k2];
      }
    }
    return pos;
  }
public static int unsignedDifference ( final short [ ] set1 , final int length1 , final short [ ] set2 , final int length2 , final short [ ] buffer ) { int pos = 0 ; int k1 = 0 , k2 = 0 ; if ( 0 == length2 ) { System . arraycopy ( set1 , 0 , buffer , 0 , length1 ) ; return length1 ; } if ( 0 == length1 ) { return 0 ; } short s1 = set1 [ k1 ] ; short s2 = set2 [ k2 ] ; while ( true ) { if ( toIntUnsigned ( s1 ) < toIntUnsigned ( s2 ) ) { buffer [ pos ++ ] = s1 ; ++ k1 ; if ( k1 >= length1 ) { break ; } s1 = set1 [ k1 ] ; } else if ( toIntUnsigned ( s1 ) == toIntUnsigned ( s2 ) ) { ++ k1 ; ++ k2 ; if ( k1 >= length1 ) { break ; } if ( k2 >= length2 ) { System . arraycopy ( set1 , k1 , buffer , pos , length1 - k1 ) ; return pos + length1 - k1 ; } s1 = set1 [ k1 ] ; s2 = set2 [ k2 ] ; } else { ++ k2 ; if ( k2 >= length2 ) { System . arraycopy ( set1 , k1 , buffer , pos , length1 - k1 ) ; return pos + length1 - k1 ; } s2 = set2 [ k2 ] ; } } return pos ; }
Compute the difference between two sorted lists and write the result to the provided output
array

@param set1 first array
@param length1 length of first array
@param set2 second array
@param length2 length of second array
@param buffer output array
@return cardinality of the difference