473,471 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C++ Binary Multiplication Program

2 New Member
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
3 8360
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
Sherin
77 New Member
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. }
Oct 28 '20 #3
Banfa
9,065 Recognized Expert Moderator Expert
@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.
Oct 28 '20 #4

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

Similar topics

14
by: amitnanda | last post by:
Hi Guys, I have a matrix multiplication program in C that multiplies two matrices. When their size is 3*3 or 800*800, the program runs fine. But above that size, I get a "segmentation fault"....
3
by: allanarendra_reddy | last post by:
hi plz send me the logic for the binary convertio program
4
by: nguser3552 | last post by:
Hello everyone, I'm wondering if someone out there knows how in a visual c++ console application how I can do the following, and man I've tried, it seems simple really: I need to open up any...
14
realin
by: realin | last post by:
hi guys i am trying to make a program to convert decimal number into binary .. i am able to do that, but the number comes inverted.. like 1101 comes like 1011 now how do i swap it off.. ...
1
by: dcatunlucky | last post by:
Ok, I have an assignment to write a program that multiplies two matrices. The matrices dimensions will be user defined as well as the numbers (floating point values) inside of them. The program must...
1
by: wuletaw | last post by:
how to write a c++ program that multiplies two binary numbers using two's complement multiplication
6
by: girishc13 | last post by:
i have written the below program for matrix mult using pointers and functions. the code is: #include<stdio.h> int M = 3,N = 2; main() { int mat1,mat2,i,j,res; void matmul(int *a,int *b,int...
1
by: sunandstars | last post by:
Hi I'm very new to C programming and I'm writing a program which will generate two random numbers, multiply them together and we have to guess the answer if it is correct or not, the problem involves...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.