2 * Copyright 2007 Google Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.zxing.common.reedsolomon;
20 * <p>This class contains utility methods for performing mathematical operations over
21 * the Galois Field GF(256). Operations use the primitive polynomial
22 * x^8 + x^4 + x^3 + x^2 + 1 in calculations.</p>
24 * <p>Throughout this package, elements of GF(256) are represented as an <code>int</code>
25 * for convenience and speed (but at the cost of memory).
26 * Only the bottom 8 bits are really used.</p>
28 * @author srowen@google.com (Sean Owen)
32 private static final int PRIMITIVE = 0x011D;
33 private static final int[] exp = new int[256];
34 private static final int[] log = new int[256];
38 for (int i = 0; i < 256; i++) {
40 x <<= 1; // x = x * 2; we're assuming the generator alpha is 2
45 for (int i = 0; i < 255; i++) {
48 // log[0] == 0 but this should never be used
55 * Implements both addition and subtraction -- they are the same in GF(256).
57 * @return sum/difference of a and b
59 static int addOrSubtract(int a, int b) {
64 * @return 2 to the power of a in GF(256)
66 static int exp(int a) {
71 * @return base 2 log of a in GF(256)
73 static int log(int a) {
75 throw new IllegalArgumentException();
81 * @return multiplicative inverse of a
83 static int inverse(int a) {
85 throw new ArithmeticException();
87 return exp[255 - log[a]];
93 * @return product of a and b in GF(256)
95 static int multiply(int a, int b) {
96 if (a == 0 || b == 0) {
105 return exp[(log[a] + log[b]) % 255];