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
18
votes
1 answer

Variable size bitset

I am practicing a question on array in which I have to find unique elements. Now for this my logic is to find the max element in the array and define the bitset for that. But problem is bitset needs a constant value so how to overcome this, below…
JackSparrow
  • 707
  • 2
  • 10
  • 24
18
votes
5 answers

What is the size of bitset in C++

I want to know how bitset actually allocates memory. I read from some blog that it takes up memory in bits. However when i run the following code: bitset<3> bits = 001; cout<
Saurabh
  • 333
  • 1
  • 2
  • 11
17
votes
9 answers

Fastest way to compare bitsets (< operator on bitsets)?

What is the most optimized way to implement a < operator for std::bitset corresponding to the comparison of the unsigned integer representation (it should work for bitsets of more than 64 bits) ? A trivial implementation would…
Vincent
  • 57,703
  • 61
  • 205
  • 388
16
votes
6 answers

Binary Serialization of std::bitset

std::bitset has a to_string() method for serializing as a char-based string of 1s and 0s. Obviously, this uses a single 8 bit char for each bit in the bitset, making the serialized representation 8 times longer than necessary. I want to store the…
Adi Shavit
  • 16,743
  • 5
  • 67
  • 137
16
votes
7 answers

How to shift an array of bytes by 12-bits

I want to shift the contents of an array of bytes by 12-bit to the left. For example, starting with this array of type uint8_t shift[10]: {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0xBC} I'd like to shift it to the left by 12-bits…
Justin Tanner
  • 14,062
  • 17
  • 82
  • 103
16
votes
4 answers

What is the reason for BitSet's size() method?

Is there a use case for the size() method on the java.util.BitSet class? I mean - the JavaDoc clearly says it's implementation dependant, it returns the size of the internal long[] storage in bits. From what it says, one could conclude that you…
Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
15
votes
2 answers

Why does std::bitset suggest more available bits than sizeof says there are?

I'm working on some simple bit manipulation problems in C++, and came across this while trying to visualize my steps. I understand that the number of bits assigned to different primitive types may vary from system to system. For my machine,…
jonthalpy
  • 1,042
  • 8
  • 22
15
votes
3 answers

Java JDK BitSet vs Lucene OpenBitSet

I was trying to implement a BloomFilter and came across some discussions regarding BitSets. The Lucene OpenBitSet claims that it is faster than the Java BitSet implementation in almost all of the…
sriram manoj
  • 155
  • 1
  • 9
15
votes
2 answers

in bitset, can i use "to_ulong" for a specific range of bits?

I'm working on something that requires me to get access to specific bits and ranges of bits. I decided to use bitset because it is easy to get access to specific bits; how can I extract a range (subset) of bits?
jimmy
  • 151
  • 1
  • 3
14
votes
2 answers

Why does Java `BitSet` not have `shiftLeft` and `shiftRight` functions?

Is there any particular reason why these are missing? They do exist in BigInteger, but due to the immutable design pattern of BigInteger these are usually awfully slow. BitSet is much nicer because it is mutable, but I really miss the shift…
Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
14
votes
4 answers

Converting from BitSet to Byte array

I have picked up this example which converts BitSet to Byte array. public static byte[] toByteArray(BitSet bits) { byte[] bytes = new byte[bits.length()/8+1]; for (int i=0; i
JavaBits
  • 2,005
  • 11
  • 36
  • 40
14
votes
2 answers

Why are the bits of a std::bitset in reverse order?

Why does bitset store the bits in reverse order? After strugging many times I have finally written this binary_to_dec. Could it simplified? int binary_to_dec(std::string bin) { std::bitset<8> bit; int c = bin.size(); for (size_t i = 0;…
user4344
  • 661
  • 3
  • 8
  • 16
14
votes
5 answers

What is the performance of STL bitset::count() method?

I searched around and could not find the performance time specifications for bitset::count(). Does anybody know what it is (O(n) or better) and where to find it? EDIT By STL I refer only to the Standard Template Library.
yasouser
  • 5,113
  • 2
  • 27
  • 41
14
votes
5 answers

Why is the internal data of BitSet in java stored as long[] instead of int[] in Java?

In java, the internal data of BitSet is stored as long[] instead of int[], I want to know why? Here is the code in jdk: /** * The internal field corresponding to the serialField "bits". */ private long[] words; If it's all about performance, I…
jerry_sjtu
  • 5,216
  • 8
  • 29
  • 42
14
votes
2 answers

Very Compact Bitarray in Java

I'm looking for a very compact way of storing a dense variable length bitarray in Java. Right now, I'm using BitSet, but it seems to use on average 1.5*n bits of storage space for a bit vector of size n. Typically, this isn't a problem, but in this…
dmcer
  • 8,116
  • 1
  • 35
  • 41
1
2
3
50 51