加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C左移大值

发布时间:2020-12-16 10:36:24 所属栏目:百科 来源:网络整理
导读:我想知道如何在C中改变左边的值. 例如: 1 180 我认为结果应该是: 1532495540865888858358347027150309183618739122183602176 (在python [1 180]中测试); 解决方法 Python支持任意精度算术,C不支持. 而且,根据Standard [expr.shift]: The behavior is unde
我想知道如何在C中改变左边的值.
例如:

1 << 180

我认为结果应该是:

1532495540865888858358347027150309183618739122183602176

(在python [1< <180]中测试);

解决方法

Python支持任意精度算术,C不支持.

而且,根据Standard [expr.shift]:

The behavior is undefined if the right operand is negative,or greater
than or equal to the length in bits of the promoted left operand.

为了在C中使用大整数,您可以使用Boost库,它使用长算术实现为不同的库提供包装:

#include <boost/multiprecision/gmp.hpp>
#include <iostream>

int main()
{
    boost::multiprecision::mpz_int one(1);
    std::cout << (one << 180) << std::endl;
    return 0;
}

打印

1532495540865888858358347027150309183618739122183602176

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读