我有一個uint128_t
類,它的值存儲爲uint64_t UPPER, LOWER;
,我不知道如何超載operator<<
,以便當我通過std::cout
時,該值將以十進制正確打印。目前,我只是在做正確打印出2個部分的整數
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
所以,如果'upper'是1和'lower'是2,它應該是什麼打印? 100,000,000,000,000,000,002?或18,446,744,073,709,551,618? – Beta 2011-05-25 20:45:47
我想我應該打印18,446,744,073,709,551,618,因爲'UPPER'中的十進制是'1 << 64' – calccrypto 2011-05-25 20:51:55