2 * Copyright 2007 ZXing authors
\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
27 private final int size;
\r
29 public BitArray(int size) {
\r
31 throw new IllegalArgumentException("size must be at least 1");
\r
34 this.bits = makeArray(size);
\r
37 public int getSize() {
\r
42 * @param i bit to get
\r
43 * @return true iff bit i is set
\r
45 public boolean get(int i) {
\r
46 return (bits[i >> 5] & (1 << (i & 0x1F))) != 0;
\r
52 * @param i bit to set
\r
54 public void set(int i) {
\r
55 bits[i >> 5] |= 1 << (i & 0x1F);
\r
59 * Sets a block of 32 bits, starting at bit i.
\r
61 * @param i first bit to set
\r
62 * @param newBits the new value of the next 32 bits. Note again that the least-significant bit
\r
63 * corresponds to bit i, the next-least-significant to i+1, and so on.
\r
65 public void setBulk(int i, int newBits) {
\r
66 bits[i >> 5] = newBits;
\r
70 * Clears all bits (sets to false).
\r
72 public void clear() {
\r
73 int max = bits.length;
\r
74 for (int i = 0; i < max; i++) {
\r
80 * Efficient method to check if a range of bits is set, or not set.
\r
82 * @param start start of range, inclusive.
\r
83 * @param end end of range, exclusive
\r
84 * @param value if true, checks that bits in range are set, otherwise checks that they are not set
\r
85 * @return true iff all bits are set or not set in range, according to value argument
\r
86 * @throws IllegalArgumentException if end is less than or equal to start
\r
88 public boolean isRange(int start, int end, boolean value) {
\r
90 throw new IllegalArgumentException();
\r
93 return true; // empty range matches
\r
95 end--; // will be easier to treat this as the last actually set bit -- inclusive
\r
96 int firstInt = start >> 5;
\r
97 int lastInt = end >> 5;
\r
98 for (int i = firstInt; i <= lastInt; i++) {
\r
99 int firstBit = i > firstInt ? 0 : start & 0x1F;
\r
100 int lastBit = i < lastInt ? 31 : end & 0x1F;
\r
102 if (firstBit == 0 && lastBit == 31) {
\r
106 for (int j = firstBit; j <= lastBit; j++) {
\r
111 // Return false if we're looking for 1s and the masked bits[i] isn't all 1s (that is,
\r
112 // equals the mask, or we're looking for 0s and the masked portion is not all 0s
\r
113 if ((bits[i] & mask) != (value ? mask : 0)) {
\r
121 * @return underlying array of ints. The first element holds the first 32 bits, and the least
\r
122 * significant bit is bit 0.
\r
124 public int[] getBitArray() {
\r
129 * Reverses all bits in the array.
\r
131 public void reverse() {
\r
132 int[] newBits = makeArray(size);
\r
133 int max = newBits.length;
\r
134 for (int i = 0; i < max; i++) {
\r
137 int size = this.size;
\r
138 for (int i = 0; i < size; i++) {
\r
139 if (get(size - i - 1)) {
\r
140 newBits[i >> 5] |= 1 << (i & 0x1F);
\r
146 private static int[] makeArray(int size) {
\r
147 int arraySize = size >> 5;
\r
148 if ((size & 0x1F) != 0) {
\r
151 return new int[arraySize];
\r
154 public String toString() {
\r
155 StringBuffer result = new StringBuffer(size);
\r
156 for (int i = 0; i < size; i++) {
\r
158 result.append(' ');
\r
160 result.append(get(i) ? 'X' : '.');
\r
162 return result.toString();
\r