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
14
votes
2 answers

Why is the std::bitset<8> variable unable to handle 11111111?

Why is this program showing the following output ? #include ... { std::bitset<8> b1(01100100); std::cout< b2(11111111); std::cout<
Shravan
  • 2,809
  • 2
  • 18
  • 39
14
votes
3 answers

Which bitset implementation should I use for maximum performance?

I'm currently trying to implement various algorithms in a Just In Time (JIT) compiler. Many of the algorithms operate on bitmaps, more commonly known as bitsets. In C++ there are various ways of implementing a bitset. As a true C++ developer, I…
Man of One Way
  • 3,904
  • 1
  • 26
  • 41
13
votes
7 answers

How to implement a bitset in C?

I have been using the Bitset class in Java and I would like to do something similar in C. I suppose I would have to do it manually as most stuff in C. What would be an efficient way to implement? byte bitset[] maybe bool bitset[] ?
David Robles
  • 9,477
  • 8
  • 37
  • 47
12
votes
6 answers

Get all Indexes of the set bits in a BitSet

I'm looking for a fast algorithm with gives me all the indexes of the set bits in a BitSet object. This is slow: BitSet bitSet = ... Collection indexes = new ArrayList(bitSet.cardinality()); int nextSetBit =…
myborobudur
  • 4,385
  • 8
  • 40
  • 61
12
votes
3 answers

Java BitSet and byte[] usage

I have this application where I should use BitSet class heavily and write to a file bit by bit. I know I can't write bits to a file, so first I convert the BitSet object to byte array and write as byte array. But the problem is since BitSet class…
gmnnn
  • 339
  • 1
  • 3
  • 19
11
votes
2 answers

Construct bitset from array of integers

It's easy to construct a bitset<64> from a uint64_t: uint64_t flags = ...; std::bitset<64> bs{flags}; But is there a good way to construct a bitset<64 * N> from a uint64_t[N], such that flags[0] would refer to the lowest 64 bits? uint64_t…
Barry
  • 286,269
  • 29
  • 621
  • 977
11
votes
2 answers

Why there isn't a single bit data type in C/C++?

For bool, it's 8 bit while has only true and false, why don't they make it single bit. And I know there's bitset, however it's not that convenient, and I just wonder why?
iloahz
  • 4,491
  • 8
  • 23
  • 31
11
votes
2 answers

Is it possible to convert bitset<8> to char in c++?

I have bitset<8> v8 and its value is something like "11001101", how can I convert it to char? I need a single letter. Like letter "f"=01100110. P.S. Thanks for help. I needed this to illustrate random errors in bits. For example without error f, and…
Van
  • 113
  • 1
  • 1
  • 5
10
votes
3 answers

How do I convert bitset to array of bytes/uint8?

I need to extact bytes from the bitset which may (not) contain a multiple of CHAR_BIT bits. I now how many of the bits in the bitset I need to put into an array. For example, the bits set is declared as std::bitset < 40> id; There is a separate…
dubnde
  • 4,359
  • 9
  • 43
  • 63
10
votes
6 answers

Convert Byte Array into Bitset

I have a byte array generated by a random number generator. I want to put this into the STL bitset. Unfortunately, it looks like Bitset only supports the following constructors: A string of 1's and 0's like "10101011" An unsigned long. (my byte…
Unknown
  • 45,913
  • 27
  • 138
  • 182
10
votes
1 answer

Large (0,1) matrix multiplication using bitwise AND and popcount instead of actual int or float multiplies?

For multiplying large binary matrices (10Kx20K), what I usually to do is to convert the matrices to float ones and perform float matrix multiplication as integer matrix multiplication is pretty slow (have a look at here). This time though, I'd need…
NULL
  • 759
  • 9
  • 18
10
votes
4 answers

Why is BitSet not Iterable?

BitSet has a stream() method but it does not implement the Iterable interface like other types that provide this method. Is there a specific reason for this?
Raffi Khatchadourian
  • 3,042
  • 3
  • 31
  • 37
10
votes
5 answers

Concatenate boost::dynamic_bitset or std::bitset

what is the best way to concatenate 2 bitsets? For example i've got boost::dynamic_bitset<> test1( std::string("1111") ); boost::dynamic_bitset<> test2( std::string("00") ); they should be concatenated into a thrid Bitset test3 which then…
MOnsDaR
  • 8,401
  • 8
  • 49
  • 70
10
votes
2 answers

Why does std::bitset expose bits in little-endian fashion?

When I use std::bitset::bitset( unsigned long long ) this constructs a bitset and when I access it via the operator[], the bits seems to be ordered in the little-endian fashion. Example: std::bitset<4> b(3ULL); std::cout << b[0] << b[1] << b[2]…
legends2k
  • 31,634
  • 25
  • 118
  • 222
10
votes
2 answers

Converting Between std::bitset and std::vector

I have a std::bitset but now I want to use an STL algorithm on it. I could have used std::vector instead, but I like std::bitset's constructor and I want std::bitset's bitwise operations. Do I have to go through a loop and stuff everything in…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
1 2
3
50 51