Questions tagged [bitset]

A collection of bits organized as an array of bits, in which each bit can be accessed separately. For C++ bitsets, prefer the tag std-bitset

A collection of bits organized as an array of bits, in which each bit can be accessed separately.

Usage guidelines:

Related tags, with slighly differences:

751 questions
66
votes
5 answers

What is the performance of std::bitset?

I recently asked a question on Programmers regarding reasons to use manual bit manipulation of primitive types over std::bitset. From that discussion I have concluded that the main reason is its comparatively poorer performance, although I'm not…
quant
  • 21,507
  • 32
  • 115
  • 211
63
votes
7 answers

Java BitSet Example

I'm looking for a good Java BitSet example to work with 0 and 1s. I tried looking at the Javadocs but I don't understand the usage of the class by just reading that. For instance, how would the and, or, and xor methods work on two different BitSet…
Steffan Harris
  • 9,106
  • 30
  • 75
  • 101
60
votes
7 answers

BitSet to and from integer/long

If I have an integer that I'd like to perform bit manipulation on, how can I load it into a java.util.BitSet? How can I convert it back to an int or long? I'm not so concerned about the size of the BitSet -- it will always be 32 or 64 bits long. …
ataylor
  • 64,891
  • 24
  • 161
  • 189
51
votes
9 answers

Auto increment in MongoDB to store sequence of Unique User ID

I am making a analytics system, the API call would provide a Unique User ID, but it's not in sequence and too sparse. I need to give each Unique User ID an auto increment id to mark a analytics datapoint in a bitarray/bitset. So the first user…
est
  • 11,429
  • 14
  • 70
  • 118
39
votes
2 answers

Why doesn't std::bitset come with iterators?

It appears that std::bitset does not come with STL iterators. Therefore, I cannot do the following: std::bitset<8> bs; for (auto it: bs) { std::cout << "this can not be done out of the box\n"; } Instead I must: std::bitset<8> bs; for…
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
38
votes
5 answers

Python equivalent to Java's BitSet

Is there a Python class or module that implements a structure that is similar to the BitSet?
Mantas Vidutis
  • 16,376
  • 20
  • 76
  • 92
37
votes
7 answers

Define bitset size at initialization?

I want to make a bitset in C++. I did a bit of research. All examples I found where like this: bitset<6> myBitset; // do something with it But I don't know the size of the bitset when I define the variable in my class: #include class…
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
32
votes
2 answers

convert bitset to int in c++

In c++. I initialize a bitset to -3 like: std::bitset<32> mybit(-3); Is there a grace way that convert mybit to -3. Beacause bitset object only have methods like to_ulong and to_string.
tenos
  • 909
  • 1
  • 9
  • 16
29
votes
6 answers

Is the use of std::vector objects in C++ acceptable, or should I use an alternative?

I'm working with a user-defined quantity of bits (I'm holding a three-dimensional array of bits, so the size increases cubically - assume no less then 512 bits), and need to flip them each individually. Right now, just on a computer, I'm using the…
Breakthrough
  • 2,444
  • 2
  • 23
  • 37
27
votes
7 answers

Efficient way of iterating over true bits in std::bitset?

Is there a way of iterating over a (possibly huge) std::bitset that is linear in the number of bits that are set to true? I want to prevent having to check every single position in the bitset. The iteration should successively return the indices of…
Astyanax
  • 271
  • 1
  • 3
  • 3
25
votes
8 answers

Shifting a Java BitSet

I am using a java.util.BitSet to store a dense vector of bits. I want to implement an operation that shifts the bits right by 1, analogous to >>> on ints. Is there a library function that shifts BitSets? If not, is there a better way than the…
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
21
votes
4 answers

C++ enum flags vs bitset

What are pros/cons of usage bitsets over enum flags? namespace Flag { enum State { Read = 1 << 0, Write = 1 << 1, Binary = 1 << 2, }; } namespace Plain { enum State { Read, Write, …
Nikolai Shalakin
  • 1,349
  • 2
  • 13
  • 25
19
votes
4 answers

Define a large bitset in C++

In my program I need to check if I have already generated a value in a set of 2.5*10^9. I expect to generate about the half of the set and need to have a fast way to check and update it. The bitset seemed to me as a good idea as it takes not too…
Martin Trigaux
  • 5,311
  • 9
  • 45
  • 58
19
votes
3 answers

Java: Count number of bits set in a java.util.BitSet

Any quick method to count the number of set bits in a BitSet other than the usual 'keep a counter' method?
Rnet
  • 4,796
  • 9
  • 47
  • 83
19
votes
5 answers

Bit field vs Bitset

I want to store bits in an array (like structure). So I can follow either of the following two approaches Approach number 1 (AN 1) struct BIT { int data : 1 }; int main() { BIT a[100]; return 0; } Approach number 2 (AN 2) int main() { …
CLOWN
  • 193
  • 1
  • 1
  • 4
1
2 3
50 51