正确打印出两部分的整数
我有一个 uint128_t
类,将其值存储为 uint64_t UPPER, LOWER;
,并且我不确定如何重载 operator<<
所以当我传入 std::cout 时,该值将正确以十进制打印。目前,我只是在做
std::ostream & operator<<(std::ostream & stream, uint128_t const & rhs){
if (rhs.upper()) // if the upper value has a non-zero digit
stream << rhs.upper();
// i need some way to pad this so that the number of 0s between
// upper and lower is correct
stream << rhs.lower();
return stream;
我应该做什么?
编辑:
示例:
如果 uint128_t 变量具有 UPPER = 1
和 LOWER = 1
,我希望流包含 (1 <<< 64) + 1
I have a uint128_t
class that stores its values as uint64_t UPPER, LOWER;
, and im not sure how to overload the operator<<
so that when i pass in std::cout
, the value will print in decimal properly. currently, im just doing
std::ostream & operator<<(std::ostream & stream, uint128_t const & rhs){
if (rhs.upper()) // if the upper value has a non-zero digit
stream << rhs.upper();
// i need some way to pad this so that the number of 0s between
// upper and lower is correct
stream << rhs.lower();
return stream;
what should i do?
edit:
example:
if a uint128_t variable has UPPER = 1
and LOWER = 1
, i want the stream to contain the decimal value of (1 << 64) + 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您正确实现了除法运算符和模运算符,您可以执行以下操作:
Assuming you have your division operator and your modulus operator properly implemented, you can do this: