bitset

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

前提是你 提交于 2019-12-03 17:11:40
问题 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 won't be able to set a bit with a higher index than size() , but that's not true, the BitSet can grow automatically: BitSet myBitSet = new BitSet(); System.out.println(myBitSet.size()); // prints "64" myBitSet.set(768); System.out.println(myBitSet.size

Bit field vs Bitset

喜夏-厌秋 提交于 2019-12-03 11:24:00
问题 I want to store bits in an array (like structure). So I can follow either of the following two approaches Approach number 1 (AN 1) struct BIT { int data : 1 }; int main() { BIT a[100]; return 0; } Approach number 2 (AN 2) int main() { std::bitset<100> BITS; return 0; } Why would someone prefer AN 2 over AN 1? 回答1: Because approach nr. 2 actually uses 100 bits of storage, plus some very minor (constant) overhead, while nr. 1 typically uses four bytes of storage per Bit structure. In general, a

Setting boost dynamic_bitset from a string

匿名 (未验证) 提交于 2019-12-03 08:48:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Dynamic bitset I have a use case where i need to populate boost::dynamic_bitset<unsigned char> , from a std::string buffer. Can you suggest as to how to go about this. So I need to come up with a function void populateBitSet (std::string &buffer, boost::dynamic_bitset<unsigned char> & bitMap) { //populate bitMap from a string buffer } 回答1: If you have binary data like this: string buffer = "0101001111011"; You want to initialize it like this (turns out there's a constructor that handles this case): void populateBitSet (std::string &buffer,

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

匿名 (未验证) 提交于 2019-12-03 08:36:05
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: The Standard Library class template std::bitset<N> 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 is not marked as explicit . What is the rationale behind this? 回答1: Explicit construction The main objection against an explicit constructor is that copy-initialization from unsigned integers no longer works constexpr auto N = 64 ; std :: bitset < N > b ( 0xDEADC0DE ); // OK, direct

How to implement a bitset in C?

匿名 (未验证) 提交于 2019-12-03 08:30:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: 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[] ? 回答1: CCAN has a bitset implementation you can use: http://ccan.ozlabs.org/info/jbitset.html But if you do end up implementing it yourself (for instance if you don't like the dependencies on that package), you should use an array of ints and use the native size of the computer architecture: #define WORD_BITS (8 *

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

心不动则不痛 提交于 2019-12-03 06:15:52
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 won't be able to set a bit with a higher index than size() , but that's not true, the BitSet can grow automatically: BitSet myBitSet = new BitSet(); System.out.println(myBitSet.size()); // prints "64" myBitSet.set(768); System.out.println(myBitSet.size()); // prints "832" In every single encounter with BitSet I have had in my life, I always wanted to

When to use STL bitsets instead of separate variables?

匿名 (未验证) 提交于 2019-12-03 02:49:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: In what situation would it be more appropriate for me to use a bitset (STL container) to manage a set of flags rather than having them declared as a number of separate (bool) variables? Will I get a significant performance gain if I used a bitset for 50 flags rather than using 50 separate bool variables? 回答1: Well, 50 bools as a bitset will take 7 bytes, while 50 bools as bools will take 50 bytes. These days that's not really a big deal, so using bools is probably fine. However, one place a bitset might be useful is if you need to pass those

Bit field vs Bitset

本秂侑毒 提交于 2019-12-03 01:50:27
I want to store bits in an array (like structure). So I can follow either of the following two approaches Approach number 1 (AN 1) struct BIT { int data : 1 }; int main() { BIT a[100]; return 0; } Approach number 2 (AN 2) int main() { std::bitset<100> BITS; return 0; } Why would someone prefer AN 2 over AN 1? Because approach nr. 2 actually uses 100 bits of storage, plus some very minor (constant) overhead, while nr. 1 typically uses four bytes of storage per Bit structure. In general, a struct is at least one byte large per the C++ standard. #include <bitset> #include <iostream> struct Bit {

BZOJ.5404.party(树链剖分 bitset Hall定理)

匿名 (未验证) 提交于 2019-12-03 00:41:02
题目链接 只有指向父节点的单向道路,所以c个人肯定在LCA处汇合。那么就成了有c条到LCA的路径,求最大的x,满足能从c条路径中各选出x个数,且它们不同。 先要维护一条路径的数的种类数,可以树剖+每条链维护一个bitset解决。用vector一条链加一个bitset,SDOI R2现场测过我记得空间还不算特别大。。当然本题数字只有1000种,一个点开一个bitset没问题。最后合并时还要通过线段树。 假设答案是x,那么c个人都要从可选特产中不重复地选x个,把每个人拆成x个点就是一个二分图完备匹配。 由Hall定理,左边集合(c*x个点)任意一个子集与右边集合相邻的点数应不小于该子集大小。因为每个人的x个点的连边相同(复制了x次),所以对每个人只需判断x个都选的子集。 c很小,2^c枚举子集。与右边集合相邻点数就是选中人的bitset的并的大小size。设枚举了s个人,那么每次枚举有 \(x*s \leq size\) 。 所以 \(x = \min\{\frac{size}{s}\}\) 。 好慢啊。。垫底了。。 学了下fwrite,然并软。 //220140kb 7192ms #include <cstdio> #include <cctype> #include <bitset> #include <algorithm> //#define gc() getchar()

BZOJ3687 简单题 【bitset】

匿名 (未验证) 提交于 2019-12-03 00:34:01
Description 小呆开始研究集合论了,他提出了关于一个数集四个问题: 1.子集的异或和的算术和。 2.子集的异或和的异或和。 3.子集的算术和的算术和。 4.子集的算术和的异或和。 目前为止,小呆已经解决了前三个问题,还剩下最后一个问题还没有解决,他决定把 这个问题交给你,未来的集训队队员来实现。 Input 第一行,一个整数n。 第二行,n个正整数,表示01,a2….,。 Output 一行,包含一个整数,表示所有子集和的异或和。 Sample Input 2 1 3 Sample Output 6 HINT 【样例解释】 6=1 异或 3 异或 (1+3) 【数据规模与约定】 ai >0,1 #include<bits/stdc++.h> using namespace std ; #define N 2000010 bitset <N> s; int ans= 0 ,sum= 0 ; int main(){ int n; scanf ( "%d" ,&n); s[ 0 ]= 1 ; for ( int i= 1 ;i<=n;i++){ int x; scanf ( "%d" ,&x); s^=(s<<x); sum+=x; } for ( int i= 1 ;i<=sum;i++) if (s[i])ans^=i; printf ( "%d" ,ans);