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];
37 for (int i = 0; i < 256; i++) {
39 x <<= 1; // x = x * 2; we're assuming the generator alpha is 2
44 for (int i = 0; i < 255; i++) {
47 // log[0] == 0 but this should never be used
54 * Implements both addition and subtraction -- they are the same in GF(256).
56 * @return sum/difference of a and b
58 static int addOrSubtract(int a, int b) {
63 * @return 2 to the power of a in GF(256)
65 static int exp(int a) {
70 * @return base 2 log of a in GF(256)
72 static int log(int a) {
74 throw new IllegalArgumentException();
80 * @return multiplicative inverse of a
82 static int inverse(int a) {
84 throw new ArithmeticException();
86 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];