2 * Copyright 2008 ZXing authors
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.qrcode.encoder;
20 * @author satorux@google.com (Satoru Takabayashi) - creator
21 * @author dswitkin@google.com (Daniel Switkin) - ported from C++
23 public final class BitVector {
26 private String bytes_;
32 // Return the bit value at "index".
33 public int at(final int index) {
34 Debug.DCHECK_LE(0, index);
35 Debug.DCHECK_LT(index, size_);
36 final uint8 byte = bytes_.at(index / 8);
37 return (byte >> (7 - (index % 8))) & 1;
40 // Return the number of bits in the bit vector.
45 // Return the number of bytes in the bit vector.
46 public int num_bytes() {
50 // Append one bit to the bit vector.
51 public void AppendBit(final int bit) {
52 Debug.DCHECK(bit == 0 || bit == 1);
53 final int num_bits_in_last_byte = size_ % 8;
54 // We'll expand bytes_ if we don't have bits in the last byte.
55 if (num_bits_in_last_byte == 0) {
58 // Modify the last byte.
59 bytes_[bytes_.size() - 1] |= (bit << (7 - num_bits_in_last_byte));
63 // Append "num_bits" bits in "value" to the bit vector.
64 // REQUIRES: 0<= num_bits <= 32.
67 // - AppendBits(0x00, 1) adds 0.
68 // - AppendBits(0x00, 4) adds 0000.
69 // - AppendBits(0xff, 8) adds 11111111.
70 public void AppendBits(final uint32 value, final int num_bits) {
71 Debug.DCHECK(num_bits >= 0 && num_bits <= 32);
72 int num_bits_left = num_bits;
73 while (num_bits_left > 0) {
74 // Optimization for byte-oriented appending.
75 if (size_ % 8 == 0 && num_bits_left >= 8) {
76 final uint8 byte = (value >> (num_bits_left - 8)) & 0xff;
77 bytes_.push_back(byte);
81 final int bit = (value >> (num_bits_left - 1)) & 1;
89 public void AppendBytes(final StringPiece &bytes) {
90 for (int i = 0; i < bytes.size(); ++i) {
91 AppendBits(bytes[i], 8);
96 public void AppendBitVector(final BitVector &bits) {
97 for (int i = 0; i < bits.size(); ++i) {
98 AppendBit(bits.at(i));
102 // Modify the bit vector by XOR'ing with "other"
103 public void XOR(final BitVector &other) {
104 Debug.DCHECK_EQ(size_, other.size());
105 for (int i = 0; i < bytes_.size(); ++i) {
106 // The last byte could be incomplete (i.e. not have 8 bits in
107 // it) but there is no problem since 0 XOR 0 == 0.
108 bytes_[i] ^= other.ToString()[i];
112 // Return the content of the bit vector as String.
113 public final String &ToString() {
117 // Return String like "01110111" for debugging.
118 public String ToASCII() {
120 result.reserve(size_);
121 for (int i = 0; i < size_; ++i) {
124 } else if (at(i) == 1) {