0

I am a programmer that needs help in understanding this concept. Let me briefly explain my situation.

The maximum signed number you can represent in the programming language I use(C++, for whoever is intersted in knowing) is $2^{63} -1$, which is $9223372036854775807$, the unsigned value, of course, would be $2^{64} -1$, or $(2^{63}) * 2$ = $18446744073709551614$.

However, it would be possible to represent larger values if the data is treated like a binary number, so, without using base 10. My problem is, I can't convert values to decimal to then comfortably do arithmetic operations, because remember, the limitation. So, how exactly do I add two unsigned binary values together? And I promise you, google did not help.

  • Can you give the meaning and an example for yor statement it would be possible to represent larger values if the data is treated like a binary number? Your maximum is valid for signed 64-bit integers. There are unsigned 64-bit integers with a range of 0..18446744073709551615 and floating numbers. – gammatester Sep 24 '17 at 09:55
  • @gammatester There are predefined datatypes in C++ with a fixed size, it can vary between each implementation but it is still fixed withing each of them. However, there is a datatype called bitset, which has got no limitation on its size, which would mean that I can represent nearly every desireable value in base 2. – Ulisse Benedetti Sep 24 '17 at 10:13
  • Also edited the question for better explanation. – Ulisse Benedetti Sep 24 '17 at 10:17
  • You can't easily add or multiply bitsets, though, you'd have to program that yourself. That would be inventing the wheel, because there are well-known (and well-tested) libraries, already, GMP being just one example. –  Sep 24 '17 at 10:18
  • You know, most programs until the recent years never used something greater than 32-bits integers and were doing fine. Sure with now 64-bits architectures 64-bit integers came more common, and this is huge enough for most needs. If really you need higher exponents use floating point, and if you need multiprecision then use specialized libraries like gmp for instance. – zwim Sep 24 '17 at 10:20

1 Answers1

0
#include <iostream>

using namespace std;

int main()
{
    unsigned long long x=9223372036854775807;
    cout << x+x << endl;
    return 0;
}

Output: 18446744073709551614