bitset

Alternative to Java Bitset with array like performance?

纵饮孤独 提交于 2019-12-10 13:33:50
问题 I am looking for an alternative to Java Bitset implementation. I am implementing a high performance algorithm and seems like using a Bitset object is killing its performance. Any ideas? 回答1: Someone here has compared boolean[] to BitSet and concluded with: BitSet is more memory efficient than boolean[] except for very small sizes. Each boolean in the array takes a byte. The numbers from runtime.freeMemory() are a bit muddled for BitSet , but less. boolean[] is more CPU efficient except for

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

家住魔仙堡 提交于 2019-12-10 02:46:53
问题 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 with error something like ♥, and so on with all text in file. In text you can see such errors clearly. 回答1: unsigned long i = mybits.to_ulong(); unsigned char c = static_cast<unsigned char>( i ); // simplest -- no checks for 8 bit bitsets

C++ enum flags vs bitset

半城伤御伤魂 提交于 2019-12-09 02:22:10
问题 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, Binary, Count }; } int main() { { unsigned int state = Flag::Read | Flag::Binary; std::cout << state << std::endl; state |= Flag::Write; state &= ~(Flag::Read | Flag::Binary); std::cout << state << std::endl; } { std::bitset<Plain::Count> state; state.set(Plain::Read); state.set(Plain::Binary); std::cout << state.to

Unordered (hash) map from bitset to bitset on boost

孤者浪人 提交于 2019-12-08 20:16:31
问题 I want to use a cache, implemented by boost's unordered_map , from a dynamic_bitset to a dynamic_bitset . The problem, of course, is that there is no default hash function from the bitset. It doesn't seem to be like a conceptual problem, but I don't know how to work out the technicalities. How should I do that? 回答1: I found an unexpected solution. It turns out boost has an option to #define BOOST_DYNAMIC_BITSET_DONT_USE_FRIENDS . When this is defined, private members including m_bits become

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

余生颓废 提交于 2019-12-08 18:48:40
问题 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<int val> struct int_ { static const constexpr value = val; }; template<size_t size> auto getBitsetSizeIMPL(std::bitset<size>) { return int_<size>{}; } template<typename BitsetType> constexpr size_t getBitsetSize() { return decltype(getBitsetSizeIMPL(BitsetType{}))::value; } When if it were static all you would have to

Defining the size of bitset using a template

喜欢而已 提交于 2019-12-08 06:49:41
问题 I have a class template <class MAX> class A { std::bitset<MAX> _mem ; } The aim of this class , is so that I can have variable length bitsets. To be used in different parts of my program. But clang complete gives me the error template argument for non type template parameter should be an expression 回答1: The bitset template expects a constant integral expression, not a type. Try this: template < size_t MAX > class A { std::bitset<MAX> _mem; }; 回答2: It's not clear exactly what you want to

static_cast / float / bitset / const weirdness

依然范特西╮ 提交于 2019-12-08 01:57:18
问题 Just a few hours ago, the following question came up: Variable cannot appear in a constant-expression Luckily for the OP, the answer provided did solve his problem, but I cannot reproduce the solution. I've attempted to simplify the code even more and I am now stuck with the following: #include <bitset> int main () { const size_t length_1 = static_cast<const size_t>(1.0f); std::bitset<length_1> bits_1; const size_t length_2 = static_cast<const size_t>(1.0f / 1.0f); std::bitset<length_2> bits

writing a BitSet to a file in java

Deadly 提交于 2019-12-07 01:45:20
问题 I have a BitSet and want to write it to a file- I came across a solution to use a ObjectOutputStream using the writeObject method. I looked at the ObjectOutputStream in the java API and saw that you can write other things (byte, int, short etc) I tried to check out the class so I tried to write a byte to a file using the following code but the result gives me a file with 7 bytes instead of 1 byte my question is what are the first 6 bytes in the file? why are they there? my question is

Fast ranking/unranking of combinations (64bit)

南笙酒味 提交于 2019-12-06 15:38:51
There is this great post https://stackoverflow.com/a/3143594/6589735 outlining an algorithm of ranking/unranking combinations. Also, there are concrete implementations in C++, e.g. here https://people.sc.fsu.edu/~jburkardt/cpp_src/combo/combo.cpp I am in need of a very fast implementation in C++, that does rank/unrank combinations encoded as unsigned long long on a x64 Haswell CPU. My attempt is in great need of improvement. unsigned long long rank(unsigned long long comb, int card) { unsigned long long rank = 0; for (int i = 1; i <= card; i++) { unsigned long index; _BitScanForward64(&index,

Fast bitset append?

橙三吉。 提交于 2019-12-06 13:36:58
I'm looking for a bitset implementation with fast bit appending, where several bits can be efficiently appended in one go. e.g. char value = 31; char n_bits = 5; fast_bitset bits; bits.append(value, n_bits); I have so far tried boost::dynamic_bitset and std::vector. Both of which are slow. Old Post I'm using boost::dynamic_bitset to pack some data. Usually I want to pack ~5 bits at a time which would result in a call like: char value = 31; char n_bits = 5; boost::dynamic_bitset<> bits; for(char n = n_bits-1; n >= 0; --n) bits.push_back((value >> n) & 1); However this seems to me quite