4

I'm wonder how to shift left value in C++. For example:

1 << 180

and I beleve that result of that should be:

1532495540865888858358347027150309183618739122183602176

(tested in python [1 << 180]);

Robert
  • 338
  • 5
  • 16
  • 7
    C++ have no native type that is 180 bits long. The biggest it have is `long long` which is usually only 64 bits. If you need arbitrary-precision arithmetic, then use a "bignum" library like [GMP](https://gmplib.org/). For a C++ library look at [Boost multiprecision](http://www.boost.org/doc/libs/1_60_0/libs/multiprecision/doc/html/index.html). – Some programmer dude Feb 19 '16 at 13:21
  • Perhaps a rethink of the code as to why you need to store such large numbers/bit patterns – Ed Heal Feb 19 '16 at 13:27

4 Answers4

7

Python supports arbitrary precision arithmetic, C++ does not.

Moreover, according to the 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.

In order to use big integers in C++ you may use Boost library, which provides wrappers to different libraries with long arithmetic implementations:

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

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

Prints

1532495540865888858358347027150309183618739122183602176
awesoon
  • 32,469
  • 11
  • 74
  • 99
7

You can do this using a std::bitset:

std::bitset<200> bits = 1; // 200 bits long
bits <<= 180;

How useful that is depends on what you want to do with it. It can't be converted to a single build-in type because they are not large enough. But there are other potentially useful operations that can be performed on it.

Galik
  • 47,303
  • 4
  • 80
  • 117
1

In C++ (as in C) a left-shift by a value larger than the number of bits in the shifted operand's type actually gives undefined behaviour.

In this case you are shifting an int value which is most likely 32 bits in size left by a value greater than 32, hence, the behaviour is undefined.

If you need to deal with integers larger than the word size on your machine, you're probably going to need to use a library. GMP is one option.

davmac
  • 20,150
  • 1
  • 40
  • 68
0

Integers (or longs) are stored in 32bits and can therefore not be shifted 180. If you need the exact value, try to write/download a class that manages big integers. Otherwise, use a double and call pow(2,180). It has an accuracy 0f 15 digits

Robert Kock
  • 5,795
  • 1
  • 12
  • 20
  • There is no guarantee that integers are stored in 32-bit units. On 64-bit system, they are stored in 64-bit units. The standard only defines the range of an integer, not its implemented size. For size specific integers, there are the `intxx_t` and `uintxx_t` types. – Thomas Matthews Feb 19 '16 at 16:55
  • A double is a poor choice because it has a limited number of bits in the mantissa and loses precision for large values, such as `pow(2,180)`. Similarly, other floating points have the same issue. Remember that floating point values are represented by three pieces: Sign, Mantissa and Exponent. – Thomas Matthews Feb 19 '16 at 16:58