This project implements basic arithmetic operations on large integers (BigInt) in Java. It provides functionality for initialization, addition, subtraction, and multiplication of BigInt values. The project simulates handling integers much larger than Java's primitive data types by breaking them down into 9-digit chunks stored in arrays.
The project supports the following operations:
- Initialization: BigInt arrays are initialized to represent large integers.
- Addition: Add two BigInt numbers and store the result.
- Subtraction: Subtract one BigInt from another, handling borrow scenarios.
- Multiplication: Multiply two BigInt numbers and store the result.
The project contains three main parts, each representing a different arithmetic operation on BigInt numbers.
- Adds two BigInt numbers and handles overflow by storing carry-over values.
- Uses 32-bit unsigned integer operations to ensure large numbers can be added.
- Subtracts one BigInt number from another, ensuring the result is non-negative.
- If necessary, it uses the 2's complement method to handle cases where borrowing is needed.
- Multiplies two BigInt numbers by iterating over each digit and calculating partial products.
- Uses shift-and-add technique, similar to how manual multiplication is performed with large numbers.
The project is divided into the following files:
-
BigIntAddition.java- Contains code for adding two BigInt numbers.
- Supports initializing a BigInt, inputting a number, and adding it to another BigInt.
-
BigIntSubtraction.java- Contains code for subtracting two BigInt numbers.
- Handles borrowing and implements the 2's complement method if required.
-
BigIntMultiplication.java- Contains code for multiplying two BigInt numbers.
- Uses partial products and sums them to get the final result.
- Java Development Kit (JDK) version 8 or higher
- A basic understanding of Java programming and handling large integers.
-
Clone the repository:
git clone https://github.com/your-username/BigIntOperations.git cd BigIntOperations -
Compile the Java files: Compile each file individually using the following command:
javac BigIntAddition.java javac BigIntSubtraction.java javac BigIntMultiplication.java
-
Run the individual Java files: You can run any of the files based on the operation you'd like to test:
For Addition:
java BigIntAddition
For Subtraction:
java BigIntSubtraction
For Multiplication:
java BigIntMultiplication
-
Input and Output: Each program will prompt for input, such as the size of the number and the number itself. It will then print the result of the corresponding arithmetic operation.
Enter the size of a you are about to insert: 18
Enter first 9 digits: 123456789
Enter next 9 digits: 987654321
Enter the size of b you are about to insert: 9
Enter first 9 digits: 111111111
Result: 000000000000000000123456900987654432
Enter the size of a you are about to insert: 18
Enter first 9 digits: 123456789
Enter next 9 digits: 987654321
Enter the size of b you are about to insert: 9
Enter first 9 digits: 111111111
Subtracting b from a:
Result: 000000000000000000012345667987654210
Enter the size of a you are about to insert: 9
Enter first 9 digits: 123456789
Enter the size of b you are about to insert: 9
Enter first 9 digits: 987654321
Multiplying...
Result: 000000000000000000121932631112635269
- Division: Implement division of BigInt numbers.
- Modulus: Implement modulus operation for BigInt numbers.
- Optimization: Improve the efficiency of the multiplication algorithm.
Enjoy working with BigInt Operations!