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

Why is the std::bitset constructor with an unsigned long long argument not marked as explicit?

The Standard Library class template std::bitset has a constructor (C++11 and onwards, unsigned long argument before C++11) constexpr bitset(unsigned long long) noexcept Contrary to many best-practice guidelines, this single-argument constructor…
10
votes
2 answers

Java BitSet which allows easy Concatenation of BitSets

I have a need for a BitSet which allows easy concatenation of multiple BitSets create a new BitSet. The default implementation doesn't have such a method. Is there any an implementation in some external library that any of you know which allows easy…
Can't Tell
  • 12,714
  • 9
  • 63
  • 91
9
votes
3 answers

Efficiently compute the hashCode for a BitSet-like implementation of Set

I wonder, how to efficiently compute the hashCode for a BitSet-like implementation of Set. The BitSet#hashCode is obviously fast to compute, rather stupid(*) and incompatible with Set#hashCode(). A fast compatible implementation could go…
maaartinus
  • 44,714
  • 32
  • 161
  • 320
9
votes
2 answers

Q: How bitset are inside?

The question is really simple (to ask), std::bitset<32> is the same thing as uint32_t for the memory? Or it's more like std::array ? I usually do something like: uint32_t index : 20; uint32_t magic : 12; So it's the same as this code…
Mathieu Van Nevel
  • 1,428
  • 1
  • 10
  • 26
9
votes
1 answer

Why is std::bitset::size non-static

I can't possibly imagine why it was chose that std::bitset::size is non-static. It makes it much harder to get a constexpr size; you have to write something like this: template struct int_ { static const constexpr value =…
Russell Greene
  • 2,141
  • 17
  • 29
9
votes
3 answers

How to convert string of binary values back to char

Example NOTE: that i am only concerned about letters. so bitset 000001 would be a or A. I have a string named s with the value "abc". I take each char of the string and convert it to binary value through the use of bitset. e.g bitset <6> b1 = s[0]; …
George
  • 93
  • 1
  • 1
  • 5
9
votes
2 answers

How to convert a range subset of bits in a C++ bitset to a number?

I have an std::bitset and the bitset type also provides a to_ulong method to translate the bitset into a number, my problem is about translating the bitset into a number while just considering a range in that bitset, I need to implement my own…
user2485710
  • 9,451
  • 13
  • 58
  • 102
9
votes
4 answers

Implementing a C style bitfield in Java

I have a problem that I am a bit stuck on and I was informed by a colleague that this would be a good place to seek help. I am trying to implement a C style bitfield in Java. Here is a rough example (I do not have the actual code in front of me at…
shadowisadog
  • 123
  • 1
  • 1
  • 5
8
votes
5 answers

Problems using a map with a bitset as a key

I am trying to create a map in C++ with bitset as a key. However the compiler generates the following error messages In file included from /usr/include/c++/4.6/string:50:0, from /usr/include/c++/4.6/bits/locale_classes.h:42, …
uyetch
  • 2,150
  • 3
  • 28
  • 33
8
votes
3 answers

Is std::bitset bit-order portable?

Does C++ say anything on bit-ordering? I'm especially working on protocol packet layouts, and I'm doubting whether there is a portable way to specify that a certain number be written into bits 5,6,7, where bit 5 is the 'most significant'. My…
xtofl
  • 40,723
  • 12
  • 105
  • 192
8
votes
2 answers

How to access range of bits in a bitset?

I have a bitset which is very large, say, 10 billion bits. What I'd like to do is write this to a file. However using .to_string() actually freezes my computer. What I'd like to do is iterate over the bits and take 64 bits at a time, turn it into…
Terence Chow
  • 10,755
  • 24
  • 78
  • 141
8
votes
1 answer

What is the complexity of C++ bitset constructor that converts from long?

My guess is O(n) where n is the no. of bits. Or is it constant w.r.t. n? I mean it shouldn’t it just be able to copy the bits from memory?
Foon
  • 221
  • 1
  • 7
8
votes
6 answers

How to reverse bits in a bitset?

For example, I have the integer a = 10; and it's binary representation (for a 32 bit integer) is 00000000000000000000000000001010 and reversed, it becomes 01010000000000000000000000000000 Now I've seen this code, from this topcoder article that…
Rockstar5645
  • 4,376
  • 8
  • 37
  • 62
8
votes
1 answer

Why std::swap does not work with std::bitset content?

Consider this unit test: std::bitset<8> temp( "11010100" ); reverseBitSet( temp ); CPPUNIT_ASSERT( temp == std::bitset<8>( "00101011" ) ); This implementation works: template static inline void reverseBitSet( std::bitset<_Count>&…
jpo38
  • 20,821
  • 10
  • 70
  • 151
8
votes
3 answers

How to convert BitSet to binary string effectively?

I am looking for an efficient way how to easily convert a BitSet to a binary string. Let us say that its usual length would be thousands of bits. For example, lets have this: BitSet bits = new BitSet(8); bits.set(1); bits.set(3); And this is the…
voho
  • 2,805
  • 1
  • 21
  • 26