// The main function that multiplies two bit strings X and Y and returns // result as long integer long int multiplyBitStrings(string X, string Y) { //length int length = (X.length()>X.length()) ? X.length() : X.length(); // Base cases if (length == 0) return 0; if (length == 1) return (a[0] - '0')*(b[0] - '0'); // Divide and Conquer int fh = length/2; // First half of string, floor(n/2) int sh = (length-fh); // Second half of string, ceil(n/2) // Find the first half and second half of first string. string a1 = X.substr(0, fh); string a2 = X.substr(fh, sh); // Find the first half and second half of second string string b1 = Y.substr(0, fh); string b2 = Y.substr(fh, sh); // Recursively calculate the three products of inputs of size n/2 long int P1 = multiplyBitStrings(a1, b1); long int P2 = multiplyBitStrings(a2, b2); long int P3 = multiplyBitStrings(addBitStrings(a1, a2), addBitStrings(b1, b2)); // Combine the three products to get the final result. return P1*(1<<(2*sh)) + (P3 - P1 - P2)*(1<<sh) + P2; }
int main() { char n1[100] = "10"; char n2[100] = "1"; char *sum = addBitStrings(n1,n2); puts(sum); return 0; }