Easiest way to print `int 128 bit` or`unsigned int 128 bit`

 To print `__int128`, you must use `__int128_t`data type. Because there is no data type named __int128. At least I didn’t find any till now.

And You must use “typecasting” every time in assigning value and outputting values.

you must use format specifier `%lld` for `__int128_t`
and `%llu`for `__uint128_t`.

in C++, `std::cout`printing…
you must use `static_cast <long long> (var_name)`

Code:

#include <iostream>
int main()
{
// int 128 bit
__int128_t var = (long long)-23542342345435;
// unsigned int 128 bit
__uint128_t var2 = (long long)64324234234234;
// printing in c++
std::cout << "int 128 bit: "
<< static_cast<long long>(var) << std::endl;
// printing in c
printf("int 128 bit: %lld\n", (long long)var);
// printing in c++
std::cout << "unsinged int 128 bit: "
<< static_cast<long long>(var2) << std::endl;
// printing in c
printf("unsinged int 128 bit: %llu\n", (long long)var2);
return 0;
}

Output:

PS D:\CodeFiles\Master-of-C-programming\Day2>
g++ v7.cpp -o v7
.\v7

int 128 bit: -23542342345435
int 128 bit: -23542342345435
unsinged int 128 bit: 64324234234234
unsinged int 128 bit: 64324234234234

previously commented on this: https://stackoverflow.com/a/78573447/6546572

Comments

Popular posts from this blog

Whiteboarding Interviews

Version Control with Git: A Beginner’s Guide

Callback function in JavaScript