我想创建一个存储 500 字节位图 ( 500 bytes * 8 bits per byte = 4000 bits) 的表列,并执行操作以改变位图中某些索引处的位(设置为 1 或 0)。
然而,有关位图的文档页面大多是空的,我只能将原始位函数作为唯一的指南。如何在 MySQL 中创建、计数、读取和更改位图作为列类型?
使用binandlpad可以将 64 位数字作为二进制字符串打印出来。
LPAD(BIN(34), 64, '0')
0000000000000000000000000000000000000000000000000000000000100010
Run Code Online (Sandbox Code Playgroud)
然而,如何打印出可能有 4000 位长的 binary/blob/varbinary 字符串呢?
(注意:不是谈论位图索引)
我需要使用超过32位的位标志(现在确切地说是33位).我试过,发现std :: bitset不能处理超过32位(ulong).我是否必须使用矢量或有一种方法使bitset工作?
我在这个项目中仅限于c ++ 98,所以我不能使用boost.
谢谢.
编辑:
我想做这样的事情:
const uint64 kBigNumber = 1LL << 33;
std::bitset<33> myBitSet;
...
switch(myBitSet) {
case kBigNumber:
// do something
...
}
Run Code Online (Sandbox Code Playgroud) 我写了以下几行:
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;
Run Code Online (Sandbox Code Playgroud)
我期望输出为:
0000
0001
0010
0011
0100
0101
....
Run Code Online (Sandbox Code Playgroud)
但循环只输出'0000'.我错过了什么基本概念?
我已经设置了以下使用Java运行的代码:
BitSet bitSet = BitSet.valueOf(new byte[] { (byte)15 });
System.out.println(bitSet);
Run Code Online (Sandbox Code Playgroud)
令我惊讶的打印
{0, 1, 2, 3} //the indexes of the 1's in the bitset
Run Code Online (Sandbox Code Playgroud)
代替
{ 4, 5, 6, 7 }.
Run Code Online (Sandbox Code Playgroud)
如果我没有弄错的话,15 in 2的补码写成00001111(带1个字节).
这让我想知道为什么BitSet会向后显示索引.有没有合理的解释?
这部分代码应该返回LittleEndian中10000000的表示,然后返回128,但它返回-128.我知道128与二进制表示中的-128相同,但为什么不能代表128?
BitSet bset = new BitSet();
bset.set(7);
byte[] bitarray = bset.toByteArray();
for (byte b: bitarray)
System.out.println(b);
Run Code Online (Sandbox Code Playgroud) 使用g ++或clang ++编译时,以下代码会产生截然不同的结果.很抱歉这个很长的例子,但我还没能做到更短.
程序应将特定位位置分配给特定类型,然后构建std::bitset包含多个类型位.
#include <bitset>
#include <iostream>
using namespace std;
using Bts = bitset<32>;
int getNextId() { static int last{0}; return last++; }
template<class T> struct IdStore{ static const int bitIdx; };
template<class T> const int IdStore<T>::bitIdx{getNextId()};
template<class T> void buildBtsHelper(Bts& mBts) {
mBts[IdStore<T>::bitIdx] = true;
}
template<class T1, class T2, class... A>
void buildBtsHelper(Bts& mBts) {
buildBtsHelper<T1>(mBts); buildBtsHelper<T2, A...>(mBts);
}
template<class... A> Bts getBuildBts() {
Bts result; buildBtsHelper<A...>(result); return result;
}
template<class... …Run Code Online (Sandbox Code Playgroud) 我正在考虑如何在以下例程中加速位测试:
void histSubtractFromBits(uint64* cursor, uint16* hist){
//traverse each bit of the 256-bit-long bitstring by splitting up into 4 bitsets
std::bitset<64> a(*cursor);
std::bitset<64> b(*(cursor+1));
std::bitset<64> c(*(cursor+2));
std::bitset<64> d(*(cursor+3));
for(int bit = 0; bit < 64; bit++){
hist[bit] -= a.test(bit);
}
for(int bit = 0; bit < 64; bit++){
hist[bit+64] -= b.test(bit);
}
for(int bit = 0; bit < 64; bit++){
hist[bit+128] -= c.test(bit);
}
for(int bit = 0; bit < 64; bit++){
hist[bit+192] -= d.test(bit);
}
}
Run Code Online (Sandbox Code Playgroud)
实际的gcc实现对bit参数进行范围检查,然后使用位掩码对-s进行范围检查.我可以在没有位集和我自己的位移/屏蔽的情况下完成它,但我相当肯定不会产生任何显着的加速(告诉我,如果我错了,为什么).
我对x86-64程序集并不是很熟悉,但我知道有 …
我BitSet在我的应用程序中使用a 并且想要检查一个方法,如果所有使用的位BitSet都设置为true.现在,我知道isEmpty()检查所有位是否设置的方法false,但是我似乎无法找到正面的情况.我知道我可以做点什么someBitSet.cardinality() == someBitSet.size(),但这看起来很笨拙.我错过了什么,或者是否有明确的理由说明为什么没有实施这样的方法,但情况恰恰相反?
我有一个函数返回bitset包含其输入的ASCII值的s 向量.
typedef bitset<64> block;
vector<block> input_to_binary(string s)
{
vector<block> v;
block blk;
int j = blk.size() - 1;
for (int i = 0; i < s.size(); i++)
{
bitset<8> b(s[i]);
for (int k = b.size() - 1; k >= 0; k--)
{
blk[j] = b[k];
j--;
}
if (i % 8 == 7 || i == s.size() - 1)
{
// either the block is full now or this is the last character of the input
v.push_back(blk); …Run Code Online (Sandbox Code Playgroud) 如此处所述,std::bitset::operator^=返回*this.从与从运营商的"通常"的解释,如+=, |=, *=人们可以合理地假定给定的std::bitset实例(具有相同的尺寸)a和b,表达(a^=b).count() 将存储按位的结果XOR在操作a,并且count() 将返回在比特数a即被设置为true.但是,如下面的最小示例所示,发生了意外情况:
#include <iostream>
#include <bitset>
int main()
{
constexpr unsigned int N=6;
std::bitset<N> a;
std::bitset<N> b;
a.flip();//111111
b[0]=1;
b[4]=1;//b is now 010001 (assuming least significan bit on the right end of the string)
std::cout<<"a=="<<a.to_string()<<std::endl;
std::cout<<"b=="<<b.to_string()<<std::endl;
std::cout<<"(a xor b) to string=="<<(a^=b).to_string()<<std::endl;
//Here is the unexpected part!
std::cout<<"(a xor b) count=="<<(a^=b).count()<<std::endl;
//Note that the …Run Code Online (Sandbox Code Playgroud)