0

I want to print integer value that converted from hexadecimal value but i only could print hexadecimal value.

#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;

    cpp_int          dsa("0xFFFFFFFFFFFFFFFF");
    cpp_int          daa("9223372036854775807");
    daa = ((daa * 64) + daa);
    cout << std::hex<<dsa <<std::showbase<< endl;
    cout <<dsa << endl;
    cout <<daa << endl;
    cout <<(int)daa << endl;
    cout <<(int128_t)daa << endl;

output

ffffffffffffffff
0xffffffffffffffff
0x207fffffffffffffbf
0x7fffffff
0x207fffffffffffffbf

How can i print max value of 128 bit type of integer ?

1
  • it worked thanks to std::dec @rafix07 Feb 17, 2020 at 14:18

1 Answer 1

2

You should use std::numeric_limits to get the max value, because that's its purpose. Your formatting issue is independent of your actual question.

#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>
#include <limits>

int main()
{
    auto max = std::numeric_limits<boost::multiprecision::int128_t>::max();
    std::cout << max << std::endl;
}
6
  • it says expected an identifier about max() @lars Feb 17, 2020 at 14:21
  • Then you did something wrong or use an old compiler. Here you can check, that the example compiles. godbolt.org/z/vHxGfZ
    – lars
    Feb 17, 2020 at 15:02
  • I use microsoft compiler maybe it could be a problem @lars Feb 17, 2020 at 15:05
  • 1
    You needed std::dec only because you used std::hex previously. std::cout is stateful. So your issue was actually not what you asked for ;) "How can i print max value of 128 bit type of integer ?" But you should really use numeric_limits to get the max value, because that's its purpose.
    – lars
    Feb 17, 2020 at 15:08
  • 2
    "But you should really use numeric_limits to get the max value, because that's its purpose." should be part of the answer. In my opinion it therefore ins not self explaining.
    – user4290866
    Feb 17, 2020 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.