How can I add and subtract 128 bit integers in C or C++ if my compiler does not support them?

后端 未结 7 752
我在风中等你
我在风中等你 2020-11-28 11:07

I\'m writing a compressor for a long stream of 128 bit numbers. I would like to store the numbers as differences -- storing only the difference between the numbers rather th

7条回答
  •  南笙
    南笙 (楼主)
    2020-11-28 11:54

    Boost 1.53 now includes multiprecision:

    #include 
    #include 
    
    // Requires Boost 1.53 or higher
    // build: g++ text.cpp
    
    int main()
    {
        namespace mp = boost::multiprecision;
    
        mp::uint128_t a = 4294967296;
        mp::uint256_t b(0);
        mp::uint512_t c(0);
    
        b = a * a;
        c = b * b;
    
        std::cout << "c: " << c << "\n";
        return 0;
    }
    

    Output:

    ./a.out
    c: 340282366920938463463374607431768211456
    

提交回复
热议问题