Questions tagged [bitvector]

A data structure for an array of single bit values, usually with emphasis on memory-efficient storage and performance.

A bit Vector stores an array of single bit values (0,1) usually in a memory-efficient (i.e. packed) way. Typical operations on a bit vector are

  • access to single bits
  • boolean operations between two bit vectors (AND, OR, XOR, NOT)
  • vector-like operations (append/prepend, insertion of bits)
  • shift operations

Implementation are often optimized to use operations provided by the CPU to perform these operations in an performant way.

References

137 questions
169
votes
12 answers

Explain the use of a bit vector for determining if all characters are unique

I am confused about how a bit vector would work to do this (not too familiar with bit vectors). Here is the code given. Could someone please walk me through this? public static boolean isUniqueChars(String str) { int checker = 0; for (int i…
user1136342
  • 3,901
  • 8
  • 25
  • 38
112
votes
6 answers

Why isn't vector a STL container?

Item 18 of Scott Meyers's book Effective STL: 50 Specific Ways to Improve Your Use of the Standard Template Library says to avoid vector as it's not an STL container and it doesn't really hold bools. The following code: vector v; bool…
P0W
  • 42,046
  • 8
  • 62
  • 107
23
votes
3 answers

C++11 vector performance issue (with code example)

I notice that vector is much slower than bool array when running the following code. int main() { int count = 0; int n = 1500000; // slower with c++ vector /*vector isPrime; isPrime.reserve(n); …
guoqing
  • 249
  • 2
  • 5
18
votes
4 answers

bitwise operations on vector

what's the best way to perform bitwise operations on vector? as i understand, vector is a specialisation that uses one bit per boolean. I chose vector for memory saving reasons. I know that there are some problems with vector
Mat
  • 2,473
  • 5
  • 23
  • 24
17
votes
6 answers

Multiple bitvectors; how to find bits that are set exactly n times?

I have a collection of four bitvectors, for example: b1 = 00001010 b2 = 10100111 b3 = 10010010 b4 = 10111110 I would like to get the masks of those bits that are set in exactly 0, 1, 2, 3 or 4 of the given bitvectors. So m0 would be the mask of…
Peter Smit
  • 24,538
  • 27
  • 105
  • 165
16
votes
3 answers

Why BitVector 32 structure is more efficient than BitArray?

What is the difference between BitArray and BitVector 32 structure and what are the advantages of BitVector 32 structure over BitArray? Why is the BitVector 32 structure more efficient than BitArray? Thanks in advance. Jay...
Jaywith.7
  • 1,639
  • 4
  • 13
  • 11
15
votes
1 answer

Asymptotically optimal way to find the sum of three elements of an array closest to a given number

In his answer to this question, John Feminella says: It's possible to do this sub-quadratically if you get really fancy, by representing each integer as a bit vector and performing a fast Fourier transform, but that's beyond the scope of this…
Yktula
  • 12,757
  • 14
  • 42
  • 69
15
votes
7 answers

How do I represent and work with n-bit vectors in Python?

In an assignment I am currently working on we need to work with bit vectors, but I am very unsure of how to do this in Python. They should be able to be from 4 bits to 20 bits. I have never worked with bit vector before, but I guess that one would…
oligofren
  • 15,352
  • 12
  • 75
  • 134
15
votes
4 answers

Are std::fill, std::copy specialized for std::vector?

When thinking about this question I start to wondering if std::copy() and/or std::fill are specialized (I really mean optimized) for std::vector. Is this required by C++ standard or, perhaps, it is common approach by C++ std library…
PiotrNycz
  • 20,687
  • 7
  • 55
  • 102
12
votes
2 answers

Could multiple proxy classes make up a STL-proof bitvector?

It's well known that std::vector does not satisfy the Standard's container requirements, mainly because the packed representation prevents T* x = &v[i] from returning a pointer to a bool. My question is: can this be remedied/mitigated when…
TemplateRex
  • 65,583
  • 16
  • 147
  • 283
11
votes
7 answers

java: sparse bit vector

Are there any well-known libraries in Java for sparse bit vectors? (And are there guidelines for how sparse is useful to use them vs. java.util.BitSet?)
Jason S
  • 171,795
  • 155
  • 551
  • 900
10
votes
2 answers

Use of term rewriting in decision procedures for bit-vector arithmetic

I am working on a project whose focus is the use of term rewriting to solve/simplify fixed-size bit-vector arithmetic problems, which is something useful to do as a prior step to some decision procedure such as those based on bit-blasting. The term…
iago
  • 277
  • 1
  • 10
9
votes
2 answers

bit vectors in c++

Recently I heard about bit vectors, but i cant really find any useful info or tutorials on this topic. Can you please suggest a book or a quick tutorial on how to implement your own bit vector classes? Thank you. ---/// i cant post answers to my own…
Vis Viva
  • 1,194
  • 4
  • 15
  • 40
8
votes
2 answers

C++ Fast Bitset Short-Circuit Bitwise Operations

A demo problem: Given two std::bitsets, a and b check if any bit is set in both a and b. There are two rather obvious solutions to this problem. This is bad because it creates a new temporary bitset, and copies values all sorts of places just to…
Travis Gockel
  • 24,743
  • 10
  • 80
  • 105
8
votes
5 answers

C/C++ Bit Array or Bit Vector

I am learning C/C++ programming & have encountered the usage of 'Bit arrays' or 'Bit Vectors'. Am not able to understand their purpose? here are my doubts - Are they used as boolean flags? Can one use int arrays instead? (more memory of course,…
Srikar Appalaraju
  • 66,073
  • 51
  • 206
  • 260
1
2 3
9 10