簡體   English   中英

關於位流的未來可能的 C++ 語言或標准庫功能的一般問題

[英]A general question about possible future C++ language or standard library features regarding bit streams

在 C++20 以后的 C++ 未來版本中或在標准庫中,是否有任何提及具有特定的bitstream對象? 例如; 我們可以有一個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和縮小的對象bitstream對象,將與所有其他的兼容stream狀物體可能是一個非常強大的和有用的工具或功能。

有沒有提到類似的東西,或者是否已經存在類似於這種性質的東西,而不必在字符串或其他類型之間來回轉換等等?


編輯-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對象將只接受來自bitset s 或其他bitstream對象的數據,但它可以將其數據傳遞給bitset s、 bitstream s 或其他stream對象。

這已經如您所願:

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

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

對於中間存儲,可以簡單地使用std::stringstream ,它也可以按照您的描述工作。

然而沒有字節序,因為操作是按位的,而不是按字節的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM