c++ - 将 cpp_int 类型导出到位时,boost::multiprecision::export_bits 丢失符号

标签 c++ boost bit

我将负号值导出到位并导回,但是当位导入符号丢失时。

typedef multiprecision::int512_t balance_value;
vector<uint8_t> byteSet;
export_bits(static_cast<boost::multiprecision::int512_t>(balance), back_inserter(byteSet), 8);

当我尝试将位导入回 int512_t 时,值的符号丢失了。

balance_value balance;
import_bits(balance, byteSet.begin(), byteSet.end());

最佳答案

这是设计使然。 documentation

Exports the absolute value of val to OutputIterator out. The function will write chunk_size bits at a time to the OutputIterator, and if msv_first is true, will write the most-significant block first. Byte and bit order within each chunk_size block is always in the machines native format. Further, each block is stored in a boost::uintmax_t when it's assigned to *out.

如果您需要可移植的表示形式,还要注意字节顺序问题。

关于c++ - 将 cpp_int 类型导出到位时,boost::multiprecision::export_bits 丢失符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40653907/

相关文章:

algorithm - 如果奇数,如何执行按位舍入到偶数?

字节和 BitArray 中的 C# 位排序

c++ - 不明白写ifstream::in的原因

c++ - boost multi_index_container中modify和modify_key的区别

c++ - 在 Windows 上使用 Boost.Asio 的半并发 ICMP ping

sql - 在 VB NET 中从 SQL 数据库中检索位列

c++ - 原子比较

c++ - 公共(public)基类打破了元组的空基类优化

c++ - 在 C++ 中使用 "else if"

c++ - 从字符串设置 boost dynamic_bitset