0

I'm trying to calculate 2 ** 128 with c++, but it overflows, and I get a value of 0. any ideas on how to calculate this?? I also need to get it on a terminal, but iostream and stdio.h do not support one I tried called __int128.

#include <cstring>

int main(){
      unsigned __int128 a = 2;
      for(int i; i < 129; i++){
            a = a * 2;
      }
      std::cout <<  a << std::endl;
}

or

#include <iostream>
#include <cstring>

int main(){
      long long unsigned int a = 2;
      for(int i; i < 129; i++){
            a = a * 2;
      }
      std::cout <<  a << std::endl;
}

are the codes that I tried.

Hal Hana
  • 25
  • 5
  • nitpick: i remember some languages use `a ** b` to denote a to the power b, but I am not aware of this being common notation. Common is `a ^ b` which unfortunately is also confusing, because `^` is a logical operator. – 463035818_is_not_an_ai Apr 22 '21 at 09:04
  • 2
    You can use something like: https://www.boost.org/ or http://sourceforge.net/projects/cpp-bigint/ to perform such computations. Look at https://stackoverflow.com/questions/1188939/representing-128-bit-numbers-in-c – Gaurav Apr 22 '21 at 09:05
  • There is also a library which provides a [uint128_t` or `uint256_t](https://github.com/calccrypto/uint256_t) – Devolus Apr 22 '21 at 09:06
  • 2 ** 128 is equivalent to binary shifting 1 left 128 times. There is no need to multiply anything or use any long math library. – user7860670 Apr 22 '21 at 09:12
  • If it's trivial work that doesn't have to be done with c++, then you may have a try with python with one line : `2 ** 128` – prehistoricpenguin Apr 22 '21 at 09:12
  • You need a bignum library. – tadman Apr 22 '21 at 09:20
  • (Using the notation of `**` meaning "to the power of"). The maximum value a 128-bit unsigned integral type can represent is `2**128 - 1`. Such a type can represent `2**128` distinct values, one of which is zero. So calculating `2**128` will overflow which (since unsigned types use modulo arithmetic) gives a result of zero. – Peter Apr 22 '21 at 11:58

2 Answers2

-1

You didn't assign value to i.

the output of the program will be very large number so what you need to do is; You can set the precision directly on std::cout and use the std::fixed format specifier.

int main() {
    
    double a = 2;
      for(int i=0; i < 129; i++){
            a = a * 2;
      }
      cout.precision(200);
      std::cout <<  a << std::endl;
    return 0;
}
  • `a ^ b` is also confusing, especially for C++ developers. `a ** b` is less common, but unambiguous. – Yksisarvinen Apr 22 '21 at 09:30
  • `^` is used in (La)TeX, Matlab, or Mathematica. But not in C/C++, where it does mean bitwise XOR. – Daniel Langr Apr 22 '21 at 09:55
  • 1
    @DanielLangr `^` is used in `TeX` (in math mode) for superscripts. It is only indirectly (since written mathematics often represents a "to the power of" using a superscript). However, in mathematical notations, superscripts can be (and practically are) used to represent anything the author desires - as long as they state the meaning. – Peter Apr 22 '21 at 12:08
-1

For calculating big numbers it is recommended to use Boost multiprecision library

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

int main() {
    cpp_int num = boost::multiprecision::pow(cpp_int(2),100);
    std::cout << "This is a big number: " << num <<std::endl;
}

It will print:

This is a big number: 1267650600228229401496703205376
pktiuk
  • 235
  • 5
  • 10