Remove getBits()
[zxing.git] / core / src / com / google / zxing / common / BitArray.java
index e5eead0..bab2a3a 100644 (file)
@@ -19,12 +19,16 @@ package com.google.zxing.common;
 /**\r
  * <p>A simple, fast array of bits, represented compactly by an array of ints internally.</p>\r
  *\r
- * @author srowen@google.com (Sean Owen)\r
+ * @author Sean Owen\r
  */\r
 public final class BitArray {\r
 \r
-  private int[] bits;\r
-  private final int size;\r
+  // TODO: I have changed these members to be public so ProGuard can inline get() and set(). Ideally\r
+  // they'd be private and we'd use the -allowaccessmodification flag, but Dalvik rejects the\r
+  // resulting binary at runtime on Android. If we find a solution to this, these should be changed\r
+  // back to private.\r
+  public int[] bits;\r
+  public final int size;\r
 \r
   public BitArray(int size) {\r
     if (size < 1) {\r
@@ -55,6 +59,15 @@ public final class BitArray {
     bits[i >> 5] |= 1 << (i & 0x1F);\r
   }\r
 \r
+  /**\r
+   * Flips bit i.\r
+   *\r
+   * @param i bit to set\r
+   */\r
+  public void flip(int i) {\r
+    bits[i >> 5] ^= 1 << (i & 0x1F);\r
+  }\r
+\r
   /**\r
    * Sets a block of 32 bits, starting at bit i.\r
    *\r
@@ -107,14 +120,11 @@ public final class BitArray {
           mask |= 1 << j;\r
         }\r
       }\r
-      if (value) {\r
-        if ((bits[i] & mask) != mask) {\r
-          return false;\r
-        }\r
-      } else {\r
-        if ((bits[i] & mask) != 0) {\r
-          return false;\r
-        }\r
+\r
+      // Return false if we're looking for 1s and the masked bits[i] isn't all 1s (that is,\r
+      // equals the mask, or we're looking for 0s and the masked portion is not all 0s\r
+      if ((bits[i] & mask) != (value ? mask : 0)) {\r
+        return false;\r
       }\r
     }\r
     return true;\r
@@ -132,11 +142,7 @@ public final class BitArray {
    * Reverses all bits in the array.\r
    */\r
   public void reverse() {\r
-    int[] newBits = makeArray(size);\r
-    int max = newBits.length;\r
-    for (int i = 0; i < max; i++) {\r
-      newBits[i] = 0;\r
-    }\r
+    int[] newBits = new int[bits.length];\r
     int size = this.size;\r
     for (int i = 0; i < size; i++) {\r
       if (get(size - i - 1)) {\r
@@ -157,7 +163,7 @@ public final class BitArray {
   public String toString() {\r
     StringBuffer result = new StringBuffer(size);\r
     for (int i = 0; i < size; i++) {\r
-      if (i % 8 == 0) {\r
+      if ((i & 0x07) == 0) {\r
         result.append(' ');\r
       }\r
       result.append(get(i) ? 'X' : '.');\r