0% found this document useful (0 votes)
3 views3 pages

Get Binary

The document is a C++ program that converts a unique number to a binary string and vice versa, ensuring the binary string is 4096 bits long. It includes functions for multiplying by 2, adding one, and reconstructing binary from a unique number. The program prompts the user for input and performs the conversion based on the selected option.

Uploaded by

newagent.131
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Get Binary

The document is a C++ program that converts a unique number to a binary string and vice versa, ensuring the binary string is 4096 bits long. It includes functions for multiplying by 2, adding one, and reconstructing binary from a unique number. The program prompts the user for input and performs the conversion based on the selected option.

Uploaded by

newagent.131
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <iostream>

#include <string>
#include <algorithm>
#include <bitset>

// Function declarations
std::string multiplyBy2(const std::string& num);
std::string addOne(const std::string& num);

// Function to ensure the binary string is 4096 bits long by padding with leading
zeros if necessary
std::string ensure4096BitLength(const std::string& binaryStr) {
if (binaryStr.size() < 4096) {
return std::string(4096 - binaryStr.size(), '0') + binaryStr;
}
return binaryStr; // If it's already 4096 bits, return as is
}

// Function to reconstruct the binary string from a large number (as a string)
std::string reconstructBinary(const std::string& uniqueNumberStr) {
std::string binaryString = "";
std::string number = uniqueNumberStr; // Start with the input string as the
current number

// Continue dividing the number by 2 until it becomes zero


while (number != "0") {
int remainder = 0;
std::string quotient = "";

// Perform long division by 2 (division by 2)


for (char digit : number) {
int currentDigit = remainder * 10 + (digit - '0');
quotient += (currentDigit / 2) + '0';
remainder = currentDigit % 2;
}

// Remove leading zeros from the quotient


quotient.erase(0, quotient.find_first_not_of('0'));

// Add the binary digit (the remainder) to the binary string


binaryString = (remainder == 1 ? '1' : '0') + binaryString;

// Update the number to be the quotient


number = quotient.empty() ? "0" : quotient;
}

return binaryString.empty() ? "0" : binaryString;


}

// Function to convert binary string to unique number (decimal form)


// We need to handle large numbers manually since the built-in data types can't
handle them.
std::string binaryToUniqueNumber(const std::string& binaryStr) {
std::string result = "0"; // Start with "0"

// Process each bit of the binary string


for (char bit : binaryStr) {
// Multiply the current result by 2 (left shift) and add the current bit (0
or 1)
result = multiplyBy2(result);
if (bit == '1') {
result = addOne(result); // If the bit is 1, we add 1
}
}

return result;
}

// Function to multiply a large number (in string form) by 2


std::string multiplyBy2(const std::string& num) {
std::string result = "";
int carry = 0;

// Process each digit from right to left


for (int i = num.size() - 1; i >= 0; --i) {
int currentDigit = (num[i] - '0') * 2 + carry;
result += (currentDigit % 10) + '0'; // Add the current digit's result
(mod 10)
carry = currentDigit / 10; // The carry for the next iteration
}

if (carry > 0) {
result += (carry + '0'); // If there's a carry, add it to the result
}

std::reverse(result.begin(), result.end()); // Reverse the string to get the


correct order
return result;
}

// Function to add 1 to a large number (in string form)


std::string addOne(const std::string& num) {
std::string result = "";
int carry = 1; // We start with the carry of 1 since we're adding 1

// Process each digit from right to left


for (int i = num.size() - 1; i >= 0; --i) {
int currentDigit = (num[i] - '0') + carry;
result += (currentDigit % 10) + '0'; // Add the current digit's result
(mod 10)
carry = currentDigit / 10; // The carry for the next iteration
}

if (carry > 0) {
result += (carry + '0'); // If there's a carry, add it to the result
}

std::reverse(result.begin(), result.end()); // Reverse the string to get the


correct order
return result;
}

int main() {
std::cout << "Enter an option (1 to convert unique number to binary, 2 to
convert binary to unique number): ";
int option;
std::cin >> option;
if (option == 1) {
// Ask the user to input the unique number as a string
std::string uniqueNumberStr;
std::cout << "Enter the unique number: ";
std::cin >> uniqueNumberStr; // Read the input number as a string

// Reconstruct the binary string


std::string binaryString = reconstructBinary(uniqueNumberStr);

// Ensure the binary string is 4096 bits long


binaryString = ensure4096BitLength(binaryString);

// Output the reconstructed binary string


std::cout << "Reconstructed 4096-bit binary string is: " << binaryString <<
std::endl;
}
else if (option == 2) {
// Ask the user to input the binary string
std::string binaryStr;
std::cout << "Enter the binary string (4096 bits): ";
std::cin >> binaryStr; // Read the input binary string

// Ensure the binary string is 4096 bits long


binaryStr = ensure4096BitLength(binaryStr);

// Convert the binary string to a unique number


std::string uniqueNumber = binaryToUniqueNumber(binaryStr);

// Output the unique number


std::cout << "Unique number from binary is: " << uniqueNumber << std::endl;
}
else {
std::cout << "Invalid option!" << std::endl;
}

return 0;
}

You might also like