2016-12-21 1 views
2

[추가] 이것은 ArrayIndexOutOfBoundsException에 관한 문제가 아닙니다. 순전히 알림 계산 알고리즘에 관한 것이므로 복제 된 것으로 표시하지 마십시오.ArrayIndexOutOfBoundsException을 일으키는 mod 계산의 수학 공식을 이해하는 방법은 무엇입니까?

전화 여기서, ArrayIndexOutOfBoundsException을, 로그이다

java.lang.ArrayIndexOutOfBoundsException : 길이 = 36; index = 120 at 에있는 java.lang.IntegralToString.convertInt (IntegralToString.java:234) 에있는 java.lang.IntegralToString.appendInt (IntegralToString.java:173) java.lang.StringBuilder.append (StringBuilder.java:139))는 예외

발생

에서 com.android.internal.telephony.ServiceStateTracker.onSignalStrengthResult (ServiceStateTracker.java:958) 에서 android.telephony.SignalStrength.toString (SignalStrength.java:1123)에서

buf [- 커서] = DIGITS [r];

질문은 (* (0x51EB851FL를 I) >>> 37) 코드 등

INT의 Q = (int)를 이해하는 방법이며;

INT의 Q = (0xCCCD * I) >>> 19;

[삭제] 왜 int q = i/10; int r = i - 10 * q;

이유 int q = (0xCCCD * i) >>> 19; int q = i/10과 같습니다.

올바른 알고리즘 인 경우 위의 알고리즘에 대한 설명에 따라 r은 120 일 수 있습니다. 아래

는 관련 코드이다 : 숫자의 시작

private static final char[] TENS = { 
    '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', 
    '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', 
    '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', 
    '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', 
    '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', 
    '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', 
    '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', 
    '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', 
    '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', 
    '9', '9', '9', '9', '9', '9', '9', '9', '9', '9' 
}; 

/** Ones [i] contains the tens digit of the number i, 0 <= i <= 99. */ 
private static final char[] ONES = { 
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
}; 
/** 
* The digits for every supported radix. 
*/ 
private static final char[] DIGITS = { 
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 
    'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 
    'u', 'v', 'w', 'x', 'y', 'z' 
}; 

/** 
    * Returns the string representation of i and leaves sb alone if sb is null. 
    * Returns null and appends the string representation of i to sb if sb is non-null. 
    */ 
    private static String convertInt(AbstractStringBuilder sb, int i) { 
     boolean negative = false; 
     String quickResult = null; 
     if (i < 0) { 
      negative = true; 
      i = -i; 
      if (i < 100) { 
       if (i < 0) { 
        // If -n is still negative, n is Integer.MIN_VALUE 
        quickResult = "-2147483648"; 
       } else { 
        quickResult = SMALL_NEGATIVE_VALUES[i]; 
        if (quickResult == null) { 
         SMALL_NEGATIVE_VALUES[i] = quickResult = 
           i < 10 ? stringOf('-', ONES[i]) : stringOf('-', TENS[i], ONES[i]); 
        } 
       } 
      } 
     } else { 
      if (i < 100) { 
       quickResult = SMALL_NONNEGATIVE_VALUES[i]; 
       if (quickResult == null) { 
        SMALL_NONNEGATIVE_VALUES[i] = quickResult = 
          i < 10 ? stringOf(ONES[i]) : stringOf(TENS[i], ONES[i]); 
       } 
      } 
     } 
     if (quickResult != null) { 
      if (sb != null) { 
       sb.append0(quickResult); 
       return null; 
      } 
      return quickResult; 
     } 

     int bufLen = 11; // Max number of chars in result 
     char[] buf = (sb != null) ? BUFFER.get() : new char[bufLen]; 
     int cursor = bufLen; 

     // Calculate digits two-at-a-time till remaining digits fit in 16 bits 
     while (i >= (1 << 16)) { 
      // Compute q = n/100 and r = n % 100 as per "Hacker's Delight" 10-8 
      int q = (int) ((0x51EB851FL * i) >>> 37); 
      int r = i - 100*q; 
      buf[--cursor] = ONES[r]; 
      buf[--cursor] = TENS[r]; 
      i = q; 
     } 

     // Calculate remaining digits one-at-a-time for performance 
     while (i != 0) { 
      // Compute q = n/10 and r = n % 10 as per "Hacker's Delight" 10-8 
      int q = (0xCCCD * i) >>> 19; 
      int r = i - 10*q; 
      buf[--cursor] = DIGITS[r]; 
      i = q; 
     } 

     if (negative) { 
      buf[--cursor] = '-'; 
     } 

     if (sb != null) { 
      sb.append0(buf, cursor, bufLen - cursor); 
      return null; 
     } else { 
      return new String(cursor, bufLen - cursor, buf); 
     } 
    } 
+0

기타 질문 : ** int q = i/10이 아닌 이유는 무엇입니까? int r = i - 10 * q; **, 비트 시프트 연산은 대개 (?) 빠릅니다. 그러나 컴파일러는 비트 시프트에서 곱셈을 수행해야하는지 아니면 다른 작업을해야하는지 더 잘 알고 있습니다. 따라서 컴파일러에서 어셈블리를 구현하는 것이 가장 좋습니다. 따라서 곱셈을 사용해야하는 경우 곱셈을 사용하려면 나눗셈을 사용하고 나누기가 필요하며 비트 쉬프트는 수행하지 말아야합니다. – rafid059

+0

정말 질문이 없습니다. 당신은 ** 당신의 ** mod 계산이 그 예외를 이끌어내는 이유를 묻고 있습니까? 아니면 ** 두 개의 완전히 독립적 인 것들을 묻고 있습니까? – GhostCat

+0

나는 이런 종류의 수식을 사용하는 이유를 이해할 수있다 "int q = (0xCCCD * i) >>> 19;" 그것은 직관적이고 어렵고 그것이하는 일과 그것이 올바르게 수행되는지 이해하는 것이 어렵습니다. – Saint

답변

1
  1. int q = (int) ((0x51EB851FL * i) >>> 37);

0x의 개수가 16 진수 표현으로 기록되는 것을 의미

L에서 숫자의 끝은 숫자가 Long 유형이 지정되었습니다. 그 이유는 (int) 캐스트가 사용되었습니다.

>>>37은이 표현식의 왼쪽에있는 숫자의 이진 표현을 오른쪽으로 37 번 이동해야 함을 의미합니다.예를 들어 :

16 >>> 2 
16 in binary is 10000. 
shift it to the right 2 times, we got 100.00 
100 in decimal system is equal to 4. 
16 >>> 2 = 4. 
  • 동일에 대한 int q = (0xCCCD * i) >>> 19;

  • 왜 IMO있는 16 진수 숫자로 이동 int q = i/10; int r = i - 10q;

  • 훨씬 더하지

      분할보다 빠르고 정확하게

      1. 올바른 알고리즘 인 경우 위의 알고리즘에 대한 설명에 따라 r은 120 일 수 있습니다.

      답변을 얻으려면 IDE에서 간단히 디버깅 할 수 있습니다.

    +0

    1. 나는 그것들이 int q = (0xCCCD * i) >>> 19와 동등한 이유를 이해할 수 없다. int q = i/10; 2.이 문제는 현재 복제 할 수 없으며 알고리즘을 호출하는 데모를 작성했습니다. 알림은 항상 10 이하입니다. 그래서 나는 너무 혼란 스럽다. – Saint

    관련 문제