0

I'm currently studying how string class methods are implemented by looking at the source code. I have noticed in several string class methods, the author have written the command that is similar to this

// Note: toffset, ooffset, or len might be near -1>>>1.

What is the author's intent?

More specifically, I don't really get what -1>>>1 means in this context

Below is an actual method that contain the command:

public boolean regionMatches(boolean ignoreCase, int toffset,
            String other, int ooffset, int len) {
        char ta[] = value;
        int to = toffset;
        char pa[] = other.value;
        int po = ooffset;

        // Note: toffset, ooffset, or len might be near -1>>>1. 
        if ((ooffset < 0) || (toffset < 0)
                || (toffset > (long)value.length - len)
                || (ooffset > (long)other.value.length - len)) {
            return false;
        }
        while (len-- > 0) {
            char c1 = ta[to++];
            char c2 = pa[po++];
            if (c1 == c2) {
                continue;
            }
            if (ignoreCase) {
                // If characters don't match but case may be ignored,
                // try converting both characters to uppercase.
                // If the results match, then the comparison scan should
                // continue.
                char u1 = Character.toUpperCase(c1);
                char u2 = Character.toUpperCase(c2);
                if (u1 == u2) {
                    continue;
                }
                // Unfortunately, conversion to uppercase does not work properly
                // for the Georgian alphabet, which has strange rules about case
                // conversion.  So we need to make one last check before
                // exiting.
                if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) {
                    continue;
                }
            }
            return false;
        }
        return true;
    }
halfer
  • 19,824
  • 17
  • 99
  • 186
Thor
  • 9,638
  • 15
  • 62
  • 137
  • 1
    It's Bit shifting. You can run that code individually – OneCricketeer Jun 20 '17 at 04:39
  • 1
    @cricket_007 Are you implying that the author meant `Integer.MAX_VALUE`? – Dawood ibn Kareem Jun 20 '17 at 04:41
  • I'm just answering the question that was asked – OneCricketeer Jun 20 '17 at 04:44
  • @cricket_007 actually, even though now I understand what >>> operator does, I'm still puzzling over what the authors' original intention is? Could you please elaborate on it a little bit more? Thanks – Thor Jun 20 '17 at 04:45
  • 1
    As a signed binary number, what's negative one? Now shift it one to the right. What's that binary value as a decimal number? – OneCricketeer Jun 20 '17 at 04:46
  • 1
    @CaptainAmerica As much as he/she might deny it, cricket_007 is implying that when the author wrote `-1>>>1`, they meant `Integer.MAX_VALUE` - that is, the largest possible `int`. – Dawood ibn Kareem Jun 20 '17 at 04:49
  • 1
    @cricket_007 e.g. for 8 bit signed binary number, negative one is 1111 ,1111. >>>1 means unsigned shift one bit to the right, so the result would be 0111,1111. which is the max positive number that can be represented by a signed 8 bits. I hope i'm correct – Thor Jun 20 '17 at 04:50
  • @DawoodibnKareem thank you for the comment. Judging from your answer, i'm glad that my understanding appears to be correct. Thanks both of you for helping out!! can't appreicate it enough! – Thor Jun 20 '17 at 04:54

0 Answers0