简体   繁体   中英

Use std::bitset to produce binary file

I am using std::bitset to give me a binary representation of a number. I now want to use this and output to std::cout using only the binary representation - I do not want the ASCII representation of std::bitset - I merely want to output my bitset as it is in memory.

bitset<32> bits = a;
cout << bits; //produces the ASCII characters for '1' and '0' depending on a

If your bitset is within the limits of the function to_ulong or to_ullong , you can use those functions and output those to a binary file directly.

If your bitset exceeds the amount that an unsigned long or unsigned long long can store, the function will throw an overflow exception. This means that you need to extract subsets of the bitset, and for each subset store the unsigned long or unsigned long long to file.

Unfortunately std::bitset does not provide a direct subset function, so you would have to write that yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM