Big Integers in C++
"Big integers" in C++ often refer to integers more significant than the range of built-in data types like int or long. The built-in data types can only represent a specific range of values and have fixed sizes. For instance, an int generally ranges from -2,147,483,648 to 2,147,483,647 on most computers.
Implementing Your Own Big Int Class:
Here is a condensed illustration of a unique BigInt class:
Code:
#include <iostream>
#include <vector>
class BigInt {
Private:
std::vector<int> digits;
Public:
// Constructor
BigInt(const std::string& num) {
for (char c: num) {
digits.push_back(c - '0');
}
}
// Addition operator
BigInt operator+(const BigInt& other) const {
BigInt result("");
int carry = 0;
int size = std::max(digits.size(), other.digits.size());
for (int i = 0; i < size; ++i) {
int sum = carry;
if (i < digits.size())
sum += digits[i];
if (i < other.digits.size())
sum += other.digits[i];
result. Digits.push_back(sum % 10);
carry = sum / 10;
}
if (carry != 0)
result. Digits.push_back(carry);
return result;
}
// Output operator
friend std::ostream& operator<<(std::ostream& os, const BigInt& num) {
for (int i = num.digits.size() - 1; i >= 0; --i)
os << num.digits[i];
return os;
}
};
int main() {
BigInt a("12345678901234567890");
BigInt b("98765432109876543210");
BigInt c = a + b;
std::cout << c << std::endl;
return 0;
}
Output:
![Big Integers in C++](https://static.tutorialandexample.com/cpp/big-integers-in-cpp1.png)
In this illustration, the BigInt class implements the addition function and stores the vast integer's digits in a vector. The operator+ overrides the addition operator for BigInt objects to combine them.
As a result of the operator's overloading of the output operator, the BigInt object is printed using std::cout.
GNU Multiple Precision Arithmetic Library(GMP):
The GMP library for arbitrary precision arithmetic is highly optimized. It offers effective routines for handling massive integer operations. Here's an easy illustration:
Code:
#include <gmp.h>
#include <iostream>
int main() {
mpz_t a, b, c;
mpz_init(a);
mpz_init(b);
mpz_init(c);
mpz_set_str(a, "12345678901234567890", 10);
mpz_set_str(b, "98765432109876543210", 10);
mpz_add(c, a, b);
gmp_printf("%Zd\n", c);
mpz_clear(a);
mpz_clear(b);
mpz_clear(c);
return 0;
}
Output:
![Big Integers in C++](https://static.tutorialandexample.com/cpp/big-integers-in-cpp2.png)
These are only a few methods for dealing with large numbers in C++. Your individual needs and tastes will determine your option. External libraries with broad capability and efficient algorithms, like Boost Multiprecision or GMP, are helpful in various situations. Creating your BigInt class might provide you with greater customization and control.
When working with big integers in C++, you can perform operations similar to regular integer types. Here are some standard functions you can perform on big integers:
- Arithmetic Operations
- Comparison Operations
- Bitwise Operations
- Modular Arithmetic Operations
- Conversion Operations
Arithmetic Operations:
You can do mathematical operations on large integers, including addition, subtraction, multiplication, and division. Most huge integer libraries include overloaded operators or member functions for these operations.
Comparison Operations:
When comparing large numbers, comparison operators like
==,!=,>, =, and >=. These operators compare the large integer values and produce a boolean response.
Bitwise Operations:
Big integers can also be susceptible to bitwise operations like left shift () and right shift (>>), as well as bitwise AND, bitwise OR, bitwise XOR, and bitwise NOT. These operations change the huge integers' component bits.
Modular Arithmetic:
Modulus, mod_inverse, and mod_pow perform modular arithmetic operations on large integers. You may determine the remainder, modular inverse, and modular exponentiation, respectively, using these functions.
Conversion:
It is possible to convert big integers into other numerical kinds like int, long, long, float, or double—the Boost-like libraries. Multiprecision offers member functions like convert_to and operator T() for type conversion.
It's crucial to remember that the precise syntax and methods for carrying out these operations may change depending on the vast integer library you use. The example mentioned above shows how to use the Boost. Multiprecision library; function names or syntax may vary in other libraries like GMP or bespoke implementations.
Significance Of Big Integers in C++
- Handling Large Numbers
- Precision and Accuracy
- Cryptographic Operations
- Scientific and Financial Applications
- Data Manipulation and Representation
- Algorithms and Problem Solving
In general, huge integers offer the tools to work with enormous numbers, preserve accuracy, and resolve challenging issues that require laborious numerical operations. They are crucial in various industries, including banking, science, algorithmic problem-solving, and encryption. You can handle huge numbers precisely and carry out complicated computations that go beyond the capability of C++'s conventional integer types by using extensive integer libraries.