我有一个由随机数生成器生成的字节数组。我想将其放入 STL 位集中。

不幸的是,Bitset 似乎只支持以下构造函数:

  1. 由 1 和 0 组成的字符串,如“10101011”
  2. 无符号长整型。(我的字节数组会更长)

我现在能想到的唯一解决方案是逐位读取字节数组并生成一串1和0。有人有更有效的解决方案吗?

有帮助吗?

解决方案

像这样的东西吗?(不确定模板魔法是否如我所期望的那样在这里起作用。我对 C++ 很生疏。)

std::bitset bytesToBitset<int numBytes>(byte *data)
{
    std::bitset<numBytes * CHAR_BIT> b;

    for(int i = 0; i < numBytes; ++i)
    {
        byte cur = data[i];
        int offset = i * CHAR_BIT;

        for(int bit = 0; bit < CHAR_BIT; ++bit)
        {
            b[offset] = cur & 1;
            ++offset;   // Move to next bit in b
            cur >>= 1;  // Move to next bit in array
        }
    }

    return b;
}

其他提示

有一个第三个构造函数 bitset<> - 它不带任何参数并将所有位设置为 0。我想你需要使用它然后遍历数组调用 set() 字节数组中的每一位都是 1。

有点暴力,但它会起作用。将每个字节内的字节索引和位偏移量转换为位集索引会有点复杂,但这并不是一点点思考(也许在调试器下运行)无法解决的。我认为它很可能比尝试通过字符串转换或流运行数组更简单、更高效。

伙计们,我花了很多时间编写一个反向函数(位集 -> 字节/字符数组)。就在那里:

    bitset<SIZE> data = ...

    // bitset to char array
    char current = 0;
    int offset = 0;
    for (int i = 0; i < SIZE; ++i) {
        if (data[i]) { // if bit is true
            current |= (char)(int)pow(2, i - offset * CHAR_BIT); // set that bit to true in current masked value
        } // otherwise let it to be false
        if ((i + 1) % CHAR_BIT == 0) { // every 8 bits
            buf[offset++] = current; // save masked value to buffer & raise offset of buffer
            current = 0; // clear masked value
        }
    }

    // now we have the result in "buf" (final size of contents in buffer is "offset")

好吧,说实话,我很无聊,并开始认为必须有一种比设置每一位更快的方法。

template<int numBytes>
std::bitset<numBytes * CHARBIT bytesToBitset(byte *data)
{
    std::bitset<numBytes * CHAR_BIT> b = *data;

    for(int i = 1; i < numBytes; ++i)
    {
        b <<= CHAR_BIT;  // Move to next bit in array
        b |= data[i];    // Set the lowest CHAR_BIT bits
    }

    return b;
}

这确实稍微快一些,至少只要字节数组小于 30 个元素(取决于传递给编译器的优化标志)。更大的数组以及移位位组所用的时间使得设置每个位的速度更快。

您可以从流中初始化位集。我不记得如何将 byte[] 放入流中,但是......

http://www.sgi.com/tech/stl/bitset.html

  bitset<12> x;

  cout << "Enter a 12-bit bitset in binary: " << flush;
  if (cin >> x) {
    cout << "x =        " << x << endl;
    cout << "As ulong:  " << x.to_ulong() << endl;
    cout << "And with mask: " << (x & mask) << endl;
    cout << "Or with mask:  " << (x | mask) << endl;
  }

这是我使用模板元编程的实现。
循环是在编译时完成的。
我采用了@strager版本,对其进行了修改以准备TMP:

  • 改变迭代的顺序(这样我就可以从中进行递归);
  • 减少使用变量的数量。

在运行时带有循环的修改版本:

template <size_t nOfBytes>
void bytesToBitsetRunTimeOptimized(uint8_t* arr, std::bitset<nOfBytes * CHAR_BIT>& result) {
  for(int i = nOfBytes - 1; i >= 0; --i) {
    for(int bit = 0; bit < CHAR_BIT; ++bit) {
      result[i * CHAR_BIT + bit] = ((arr[i] >> bit) & 1);
    }
  }
}

基于它的TMP版本:

template<size_t nOfBytes, int I, int BIT> struct LoopOnBIT {
  static inline void bytesToBitset(uint8_t* arr, std::bitset<nOfBytes * CHAR_BIT>& result) {
    result[I * CHAR_BIT + BIT] = ((arr[I] >> BIT) & 1);
    LoopOnBIT<nOfBytes, I, BIT+1>::bytesToBitset(arr, result);
  }
};
// stop case for LoopOnBIT
template<size_t nOfBytes, int I> struct LoopOnBIT<nOfBytes, I, CHAR_BIT> {
  static inline void bytesToBitset(uint8_t* arr, std::bitset<nOfBytes * CHAR_BIT>& result) { }
};

template<size_t nOfBytes, int I> struct LoopOnI {
  static inline void bytesToBitset(uint8_t* arr, std::bitset<nOfBytes * CHAR_BIT>& result) {
    LoopOnBIT<nOfBytes, I, 0>::bytesToBitset(arr, result);
    LoopOnI<nOfBytes, I-1>::bytesToBitset(arr, result);
  }
};
// stop case for LoopOnI
template<size_t nOfBytes> struct LoopOnI<nOfBytes, -1> {
  static inline void bytesToBitset(uint8_t* arr, std::bitset<nOfBytes * CHAR_BIT>& result) { }
};

template <size_t nOfBytes>
void bytesToBitset(uint8_t* arr, std::bitset<nOfBytes * CHAR_BIT>& result) {
  LoopOnI<nOfBytes, nOfBytes - 1>::bytesToBitset(arr, result);
}

客户端代码:

uint8_t arr[]={0x6A};
  std::bitset<8> b; 
  bytesToBitset<1>(arr,b);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top