bitset

how to make a bit-set/byte-array conversion in c

我是研究僧i 提交于 2019-12-24 04:04:42
问题 Given an array, unsigned char q[32]="1100111..." , how can I generate a 4-bytes bit-set, unsigned char p[4] , such that, the bit of this bit-set, equals to value inside the array, e.g., the first byte p[0]= "q[0] ... q[7]"; 2nd byte p[1]="q[8] ... q[15]", etc. and also how to do it in opposite, i.e., given bit-set, generate the array? my own trial out for the first part. unsigned char p[4]={0}; for (int j=0; j<N; j++) { if (q[j] == '1') { p [j / 8] |= 1 << (7-(j % 8)); } } Is the above right?

Bitset as the return value of a function

风流意气都作罢 提交于 2019-12-23 23:55:21
问题 I'd like to have an interface whose function returns a bitset: class IMyInterface { public: virtual std::bitset<100> GetBits() = 0; }; The problem is that I don't want to force the size of the bitset . So I think I have to use boost::dynamic_bitset instead: class IMyInterface { public: virtual boost::dynamic_bitset<> GetBits() = 0; }; I have heard that boost::dynamic_bitset is slower than std::bitset though. Is there any other way to avoid using dynamic_bitset and have an interface that

How to bitwise operate on memory block (C++)

我的梦境 提交于 2019-12-23 16:14:23
问题 Is there a better (faster/more efficient) way to perform a bitwise operation on a large memory block than using a for loop? After looking it to options I noticed that std has a member std::bitset , and was also wondering if it would be better (or even possible) to convert a large region of memory into a bitset without changing its values, then perform the operations, and then switch its type back to normal? Edit / update: I think union might apply here, such that the memory block is allocated

bitsets binary AND operation

荒凉一梦 提交于 2019-12-23 12:45:45
问题 I wrote the following lines: std::bitset<4> bitvec; //bitset 0000 std::bitset<4> addition; //bitset 0000 addition.set(0); //setting the least significant bit std::cout << addition << std::endl; //output 0001 std::cout << std::endl; for(int x = 0; x != 16; ++x) { //addition loop std::cout << bitvec << std::endl; //output bitvec &= addition; //binary AND } std::cout << std::endl; and I expected the output to be: 0000 0001 0010 0011 0100 0101 .... But the loop just outputs '0000'. What basic

Exclusive or between N bit sets

无人久伴 提交于 2019-12-23 10:59:06
问题 I am implementing a program in Java using BitSets and I am stuck in the following operation: Given N BitSets return a BitSet with 0 if there is more than 1 one in all the BitSets, and 1 otherwise As an example, suppose we have this 3 sets: 10010 01011 00111 11100 expected result For the following sets : 10010 01011 00111 10100 00101 01000 expected result I am trying to do this exclusive with bit wise operations, and I have realized that what I need is literally the exclusive or between all

Problems using a map with a bitset as a key

不羁岁月 提交于 2019-12-23 10:57:09
问题 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, from /usr/include/c++/4.6/bits/ios_base.h:43, from /usr/include/c++/4.6/ios:43, from /usr/include/c++/4.6/ostream:40, from /usr/include/c++/4.6/iostream:40, from test2.cpp:1: /usr/include/c++/4.6/bits/stl_function.h: In member function ‘bool std::less<_Tp>::operator()

Which bitset implementation should I use for maximum performance?

北城以北 提交于 2019-12-23 06:58:51
问题 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 would prefer to use something from the STL. The most important aspect is performance. I don't necessarily need a dynamically resizable bitset. As I see it, there are three possible options. I. One option would be to use std::vector<bool> , which has

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

徘徊边缘 提交于 2019-12-23 06:58:47
问题 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 wonder why long[] storage will get better performance. 回答1: When querying or manipulating a single bit, there is no significant difference. You have to calculate the word index and read that word and, in case of an update, manipulate one bit of that

Fast ranking/unranking of combinations (64bit)

六眼飞鱼酱① 提交于 2019-12-23 03:37:13
问题 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) {

Fast bitset append?

大憨熊 提交于 2019-12-22 17:48:28
问题 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