__int128

在很多题目里面要求高精度,但是这种高精度又不是那种非常大的,可能比unsigned longlong大,所以这个时候去写高精度模板非常不划算,用__int128代替就非常不错;

但是__int128对cin,cout,print,scanf都不支持,要另写输入输出;

inline __int128 read(){//输入模板 
    __int128 x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
inline void print(__int128 x){//输出模板 
    if(x<0){
        putchar('-');
        x=-x;
    }
    if(x>9) print(x/10);
    putchar(x%10+'0');
}

#include <iostream>

using namespace std;

void myitoa(__int128_t v, char* s)
{
    char temp;
    int i=0, j;

    while(v >0) {
        s[i++] = v % 10 + '0';
        v /= 10;
    }
    s[i] = '\0';

    j=0;
    i--;
    while(j < i) {
        temp = s[j];
        s[j] = s[i];
        s[i] = temp;
        j++;
        i--;
    }
}

int main()
{
    __uint128_t n = 0;

    n = ~n;
    int count = 0;
    while(n > 0) {
        count++;
        n >>= 1;
    }

    cout << "count=" << count << endl;
    cout << "__uint128_t size=" << sizeof(__uint128_t) << endl;
    cout << endl;

    cout << "__int128_t size=" << sizeof(__int128_t) << endl;

    __int128_t x = 1100000000000000L;
    __int128_t y = 2200000000000000L;
    char s[40];

    x *= y;

    myitoa(x, s);

    cout << "x=" << s << endl;

    return 0;
}
#include <bits/stdc++.h>
using namespace std;
inline __int128 read()
{
    __int128 x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}

inline void write(__int128 x)
{
    if(x<0)
    {
        putchar('-');
        x=-x;
    }
    if(x>9)
        write(x/10);
    putchar(x%10+'0');
}

int main()
{
    __int128 a = read();
    __int128 b = read();
    write(a + b);
    return 0;
}
### 关于 `__int128` 和 `__int128_t` 的区别及用法 #### 定义与支持 在 GCC 编译器中,`__int128` 是一种扩展整数类型,用于表示 128 位有符号整数。这种类型的引入并不强制改变大多数编译器的行为[^3]。 对于 `__int128_t`,实际上这不是标准 C++ 类型的一部分;相反,在某些情况下它可能是特定平台或库定义的一个 typedef 或宏来指代 `__int128` 类型。因此,当提到 `__int128_t` 时,通常是指向 `__int128` 的别名。 #### 使用方法 下面展示了如何声明并使用这两种类型: ```cpp #include <iostream> #include <type_traits> // 声明 __int128 变量 void use_int128() { __int128 bigNumber = 1; std::cout << "Size of __int128: " << sizeof(bigNumber) * 8 << "-bit\n"; } // 如果存在,则可以这样使用 __int128_t #ifdef __INT128_T__ void use_int128t() { __int128_t anotherBigNumber = 1; std::cout << "Size of __int128_t: " << sizeof(anotherBigNumber) * 8 << "-bit\n"; // 检查两者是否相同 static_assert(std::is_same<__int128, __int128_t>::value, "__int128 and __int128_t should be the same type"); } #endif int main(){ use_int128(); #ifdef __INT128_T__ use_int128t(); #endif return 0; } ``` 这段代码首先尝试直接使用 `__int128` 来创建变量,并打印其大小。接着通过预处理器指令判断是否存在 `__int128_t` 类型,如果存在则继续测试该类型以及验证它们之间的一致性。 需要注意的是,由于 `__int128_t` 并不是跨平台的标准定义,所以在实际编程实践中应当谨慎处理不同平台上可能存在的差异[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值