0

bitstream在 C++ 20 之后的 C++ 未来版本中或在标准库中是否有提及具有特定对象?例如; 我们可以有一个std::bitset<n>对象,其中n是一个常数已知的整数值。

伪示例:

#include <bitstream>

template<unsigned N>
class Foo {
public:
     std::bitset<N> bits_;
};

这里我们有一个类模板,它将指定对象中有多少位std::bitset。这很好。

我们也有stream诸如iostreamfstreamsstream等对象,我们可以将各种数据类型的值输入这些流,读取或写入字符串流、文件流、控制台或终端的 I/O 流等。 ..


我想知道的是,是否有任何建议具有像上面那样的流对象,但特定于类似于性质的任意位序列std::bitset,您可以将一组位输入到流中。例如,它可能看起来像这样:

#include <bitset>
#include <bitstream> // not an actual library - only suggestive

int main() {
    std::bitset<8>  byte{0xFE};
    std::bitset<16> word{0xFEDC};
    std::bitset<32> dword{0xFEDCBA98};
    std::bitset<64> qword{0xFEDCBA9876543210};

    std::bitstream bs;
    bs << byte << word << dword << qword;
    std::cout << bs; 
    return 0;
}

这里的预期输出将是:

11111110111111101101110011111110110111001011101010011000111111101101110010111010100110000111011001010100001100100001000

根据预期用途的字节序表示法...


逆转也适用。如果我们有一个已经填充的bitstream对象,例如上面示例中的对象,我们可以从以下示例中提取特定的位序列bitstream

#include <bitset>
#include <bitstream> // not an actual library - only suggestive
#include <iomanip>  // manipulators for bitstreams not yet implemented...

int main() {
    std::bitset<8>  byte{0xFE};
    std::bitset<16> word{0xFEDC};
    std::bitset<32> dword{0xFEDCBA98};
    std::bitset<64> qword{0xFEDCBA9876543210};

    std::bitstream bs;
    bs << byte << word << dword << qword;

    std::bitset<3> odd3;
    std::bitset<5> odd5;
    std::bitset<7> odd7;

    bs >> bs::msb >> odd3 >> odd7 >> odd5;
    std::cout << odd3 << '\n' << odd7 << '\n' << odd5;

    bs >> bs::lsb >> odd5 << odd3 << odd7;
    std::cout << odd5 << '\n' << odd3 << '\n' << odd7;

    // Or by using a constructor with a value for the bit size...
    std::bitset<9> odd9( bs ); // odd9(bs::msb), odd9(bs::lsb)... 

    return 0;
}

并且预期的结果将分别从 MSB 或 LSB 方向拉取bitset对象大小的数量。


就像iostream其他流库一样,通过使用该<iomanip>库,我们可以拥有格式修饰符,可以将此输入或输出分解bit-segments为任何指定长度的离散值,例如每 4 或 8 位,或将其显示为十六进制等......位操作和计算仍然是std::bitset.

能够在与所有其他类似对象兼容的对象bitset中推入或检索任意对象可能是一个非常强大且有用的工具或功能。bitstreamstream

有没有提到这样的东西,或者是否已经存在类似于这种性质的东西,而不必在字符串或其他类型之间来回转换等?


EDIT -User- walnut在他的回答中给出了一些很好的例子,并且已经可以使用现有的iostream对象和运算符来完成。您可以阅读他的回答和讨论中的评论,但是,我只是想说明我对一个stream对象库的推理,该对象库被编写和设计为明确且专门用于bitset数据类型。

想象一个这样的函数:

// Not written with perfect syntax or compilation in mind,
// only psuedo to illustrate a point.
template<typename... T>
std::ostream& concatinateBitSequences( T&& ... t ) { // assuming all t are bitset<n>
    std::stringstream sstream;
    sstream << t...;
    return sstream; 
}

并比较一下...

// Not written with perfect syntax or compilation in mind,
// only psuedo to illustrate a point.
template<typename... T>
std::bitstream& concatinateBitSequences( T&& ... t ) { // assuming all t are bitset<n>
   std::bitstream bs;
   bs << t...;
   return bs;
}

调用者,用户将知道它正在返回一个bitstream显式或专门与任意对象进行比较的stream对象!这是我的主要意图或目标......它更倾向于可读性,并且在显示正确意图的代码中更具表现力。此外,它将与bitset对象紧密耦合,这意味着bitstream对象将只接受来自bitsets 或其他bitstream对象的数据,但它可以将其数据传递给bitsets、bitstreams 或其他stream对象。

4

1 回答 1

1

This already works as you intend:

std::cout << byte << word << dword << qword;

and

std::cin >> odd5 >> odd3 >> odd7;

For intermediate storage one can simply use std::stringstream which will also work as you describe.

There is however no endianess, because operations are bitwise, not bytewise.

于 2020-02-25T07:56:28.533 回答