By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,385 Members | 1,109 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,385 IT Pros & Developers. It's quick & easy.

C++ Binary Multiplication Program

P: 2
Hello! I'm writing a program that multiplies binary numbers but i want to avoid using Booth algorithm.I'm trying to stick to the oldfashioned way of writing a line of 0's if the digit of the multiplier is 0 and writing the multiplicant again if the digit digit is 1, and make the sum of all those results to obtain the result of the multiplication.Now, the question is, how should i write the code to tell the program to look at each digit of the multiplier? I would really appreciate the help since it's a project for school.
Oct 13 '13 #1
Share this Question
Share on Google+
3 Replies

weaknessforcats
Expert Mod 5K+
P: 9,197
You would use the bit shift right operator >>. So rather than look at each bit in the multiplier, you shift the multiplier to the right by one bit on each cycle of the loop. The effect is the entire multiplier is seen by looking at the rightmost bit only.
Oct 14 '13 #2

P: 55
Approach-Just convert the binary nos into decimal form and multiply using standard norms.

Code starts here

Expand|Select|Wrap|Line Numbers
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5.     int t;
  6.     cin>>t;
  7.     while(t>0)
  8.     {   string s1,s2;
  9.       cin>>s1;
  10.       cin>>s2;
  11.       int m=0;
  12.       int n=0;
  13.       for(int i=0;s1[i]!='\0';i++)
  14.       {
  15.           n=n+1;
  16.       }
  17.       for(int i=0;s2[i]!='\0';i++)
  18.       {
  19.           m=m+1;
  20.       }
  21.       int k=0;
  22.       //char x;
  23.       int q;
  24.       for(int i=n-1;i>=0;i--)
  25.       {
  26.           q=s1[i]-48;
  27.           k=k+pow(2,n-1-(i))*q;
  28.  
  29.       }
  30.       int l=0;
  31.        for(int i=m-1;i>=0;i--)
  32.       {
  33.           q=s2[i]-48;
  34.           l=l+pow(2,m-1-(i))*q;
  35.  
  36.       }
  37.       cout<<l*k<<endl;
  38.  
  39.         t=t-1;
  40.  
  41.     }
  42.     return 0;
  43. }
4 Weeks Ago #3

Banfa
Expert Mod 5K+
P: 8,996
@Sherin I believe you have misunderstood the question, as I do not believe that the initial input for the problem was ever intended to be a string of binary digits but rather I think the original op was interested in manipulation of integers at the binary level. We can never know for sure since the question is 7 years old and I doubt the OP is coming back.

However looking at your solution for having 2 strings of binary digits then here are a few comments

Expand|Select|Wrap|Line Numbers
  1. for(int i=0;s1[i]!='\0';i++)
  2. {
  3.   n=n+1;
  4. }
You are trying to calculate the number of characters in the string; however std::string does not store a '\0' terminator in its strings (or at least there is no requirement in the standard to do that). If you want to iterate through every character of a string then use an iterator

Expand|Select|Wrap|Line Numbers
  1. for(auto iter = s1.begin(); iter != s1.end(); ++iter)
  2.   ...
If you really need to have the index of the character then the correct stop condition is the length of the string not the value of the last character

Expand|Select|Wrap|Line Numbers
  1. for(int i=0; i < s1.length(); i++)
  2.   ...
Talking of which your code is just calculating the length so just call s1.length().

Expand|Select|Wrap|Line Numbers
  1. for(int i=n-1;i>=0;i--)
  2. {
  3.   q=s1[i]-48;
  4.   k=k+pow(2,n-1-(i))*q;
  5. }
You are calculating the value of the string as an integer. Firstly you have a magic number, 48, that means your code is assuming that the execution character set is ASCII. If you want to subtract the value of the character '0' then subtract the value of the character '0' without making any assumptions about what character encoding is in use q=s1[i]-'0';
.

Worst this is complex and inefficient due to all those unnecessary calls to pow; the pseudo code to convert a string of digits in base N into an integer does not need to call pow and looks something like this

Expand|Select|Wrap|Line Numbers
  1. string text    // a string of digits in base N
  2. integer base   // The base, N, of the string of digits
  3. integer result = 0
  4.  
  5. foreach digit in text    // Note strings are read left to right 
  6.                          // so we get the most significant digit first
  7.   result = result * base // Another digit so multiply by the number base
  8.   result = result + digit - '0' // Add the value of this digit
No need for any calls to pow or to know the length of the string ahead of time although you may wish to check that
  1. All the the characters in the string are valid for the base
  2. There aren't so many digits in the string that you will cause overflow in the integer

Finally if this really was what was required then you can just call standard library function int std::stoi (const string& str, size_t* idx = 0, int base = 10);

Reducing the entire program to

Expand|Select|Wrap|Line Numbers
  1. #include<string>
  2. #include<iostream>
  3. using namespace std;
  4.  
  5. int main() {
  6.     int t;
  7.     cin>>t;
  8.     while(t>0)
  9.     {
  10.       string s1,s2;
  11.       cin>>s1;
  12.       cin>>s2;
  13.       int k=stoi (s1, nullptr, 2);
  14.       int l=stoi (s2, nullptr, 2);
  15.       cout<<l*k<<endl;
  16.  
  17.       t=t-1;
  18.     }
  19.     return 0;
  20. }
Although that code could bear having some error checking added.
4 Weeks Ago #4

Post your reply

Sign in to post your reply or Sign up for a free account.