2 * Copyright 2007 Google Inc.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 package com.google.zxing.common;
\r
20 * <p>A simple, fast array of bits, represented compactly by an array of ints internally.</p>
\r
22 * @author srowen@google.com (Sean Owen)
\r
24 public final class BitArray {
\r
29 public BitArray(int size) {
\r
31 this.bits = makeArray(size);
\r
35 * @param i bit to get
\r
36 * @return true iff bit i is set
\r
38 public boolean get(int i) {
\r
39 return (bits[i >> 5] & (1 << (i & 0x1F))) != 0;
\r
45 * @param i bit to set
\r
47 public void set(int i) {
\r
48 bits[i >> 5] |= 1 << (i & 0x1F);
\r
52 * Sets a block of 32 bits, starting at bit i.
\r
54 * @param i first bit to set
\r
55 * @param newBits the new value of the next 32 bits. Note again that the least-significant bit
\r
56 * corresponds to bit i, the next-least-significant to i+1, and so on.
\r
58 public void setBulk(int i, int newBits) {
\r
59 bits[i >> 5] = newBits;
\r
63 * Clears all bits (sets to false).
\r
65 public void clear() {
\r
66 int max = bits.length;
\r
67 for (int i = 0; i < max; i++) {
\r
73 * @return underlying array of ints. The first element holds the first 32 bits, and the least
\r
74 * significant bit is bit 0.
\r
76 public int[] getBitArray() {
\r
81 * Reverses all bits in the array.
\r
83 public void reverse() {
\r
84 int[] newBits = makeArray(size);
\r
85 int max = newBits.length;
\r
86 for (int i = 0; i < max; i++) {
\r
89 for (int i = 0; i < size; i++) {
\r
90 if (this.get(size - i - 1)) {
\r
91 newBits[i >> 5] |= 1 << (i & 0x1F);
\r
97 private int[] makeArray(int size) {
\r
98 int arraySize = size >> 5;
\r
99 if ((size & 0x1F) != 0) {
\r
102 int[] result = new int[arraySize];
\r