Skip to main content

C++ program to implement Booth's multiplier

  1  #include <iostream>
  2  #include <windows.h>
  3  #include <fstream>
  4  #include <math.h>
  5  #define pause system("pause");
  6  using namespace std;
  7  const int no_of_bits = 32;
  8  /* this Program uses Following notations.... first bit for signed and then it goes like this 1 2 4 8 16 32 64......
  9  /* ----------------------------------------global variables ----------------------------------
 10  */
 11  int Q1=0; /// extra bit holder
 12  int A[no_of_bits]; /// Accumulator storing array
 13  int Q[no_of_bits];/// Multiplier storing Array.
 14  int N,Qsize;/// no of bits in multiplier
 15  int X; /// no of bits in multiplicand.
 16  int M[no_of_bits]; /// M value
 17  int M2Complement[no_of_bits];/// -M
 18  int temp[no_of_bits]; /// for temporary use
 19  ofstream out;
 20 
 21  /*
 22  ...............................................now function ....................................
 23 
 24  */
 25  int binary_convert(int,int m[]);
 26  void ARS(); /// arithmetic Right Shift ...
 27 
 28  void Add(int[],int []); /// used for Function two's complement... and adding of two array
 29  void intialiser(int ,int ); /// used to initialize A Q N and all
 30  a
 31 
 32  /*
 33 
 34 
 35  Main Function Starts
 36 
 37 
 38  */
 39  int main ()
 40  {int multiplicand,multiplier,result=0;
 41  out.open("Booth's Multiplier.txt",ios::out);
 42  out<<"++++++++++++++++++++++++++++++++++++++++Welcome To booth's Multiplier++++++++++++++++++++++++++++\n";
 43  cout<<endl;
 44  /// m is the multiplicand
 45  /// is multiplier....
 46  cout<<"                          Booth's multiplier                                 \n\n\n\a\n";
 47  cout<<"============================================================================\n";
 48  cout<<"Enter the Multiplicand ---> ";
 49  out<<"\n\n\n\n============================================================================\n";
 50  out<<"Enter the Multiplicand ---> ";
 51 
 52  cin>>multiplicand;
 53  out<<multiplicand;
 54  cout<<"Enter the Multiplier ---> ";
 55  cin>>multiplier;
 56  cout<<"Displaying Result of "<<multiplicand<<"X"<<multiplier<<endl;
 57  out<<"\nEnter the Multiplier ---> "<<multiplier<<endl;;
 58  out<<"Displaying Result of "<<multiplicand<<"X"<<multiplier<<endl;
 59 
 60  out<<"============================================================================\n";
 61 
 62  cout<<"============================================================================\n";
 63 
 64  int opr=0;
 65  /*
 66  opr code ...
 67  opr code =1 shifting
 68  opr =2 Subs
 69  opr=3 Add...
 70  */
 71  int counter=0;
 72  intialiser(multiplicand,multiplier);
 73 
 74 
 75 
 76  cout<<"============================================================================\n\n                ::StepS::\n\n";
 77  cout<<"============================================================================\n";
 78 
 79  out<<"============================================================================\n\n                ::StepS::\n\n";
 80  out<<"============================================================================\n";
 81 
 82 
 83 
 84  cout<<"Index\t A\t\tQ\t\tQ1\tN\t operation\n\n";
 85  cout<<"0\t";
 86  out<<"Index\t A\t\tQ\t\tQ1\tN\t operation\n\n";
 87  out<<"0\t";
 88  for(int i=no_of_bits-X;i<no_of_bits;i++){
 89  cout<<A[i];out<<A[i];}cout<<"\t\t"; out<<"\t\t";
 90  for(int i=no_of_bits-N;i<no_of_bits;i++){
 91  cout<<Q[i];out<<Q[i];}
 92  cout<<"\t\t"<<Q1<<"\t"<<N<<"\t ------------";; out<<"\t"<<Q1<<"\t"<<N<<"\t -------------";
 93 
 94 
 95 
 96 
 97 
 98  while(N>0){
 99 
100 
101 
102 
103  if(Q[no_of_bits-1]==1&&Q1==0) /// it is of form 10
104  {Add(A,M2Complement);opr=2;
105 
106  }
107  else if (Q[no_of_bits-1]==0&&Q1==1) /// it is of form 01
108  {
109      Add(A,M);opr=3;
110  }
111  ARS(); /// shifting at last..
112   cout<<"\n\a____________________________________________________________________________________________________________________\n";
113  cout<<++counter<<"\t";
114  out<<"\n\a____________________________________________________________________________________________________________________\n";
115  out<<++counter<<"\t";
116 
117 
118  for(int i=no_of_bits-X;i<no_of_bits;i++){out<<A[i];
119  cout<<A[i];} cout<<"\t\t";out<<"\t\t";
120  for(int k=(no_of_bits-Qsize);k<no_of_bits;k++) {cout<<Q[k];out<<Q[k];}
121  cout<<"\t\t"<<Q1<<"\t"<<N<<"\t";out<<"\t\t"<<Q1<<"\t"<<N<<"\t";
122  if(opr==1){cout<<"\tShifting"<<endl;out<<"\tShifting"<<endl;}
123  else if(opr==2){ cout<<"\tSubtracting and Arithmetic Shifting....\n";out<<"\tSubtracting and Arithmetic Shifting....\n";}
124  else {cout<<"\tAdding and Shifting....\n";out<<"\tAdding and Shifting....\n";}
125  out<<"\n\a____________________________________________________________________________________________________________________\n";
126  cout<<"\n\a____________________________________________________________________________________________________________________\n";
127 
128  }
129  int power=0;
130  if(A[0]==0) /// it is a +ve result
131  {
132 
133      for(int k=no_of_bits-1; k>=(no_of_bits-Qsize);k--)
134 
135  {result=result+(pow(2,power++)*Q[k]);
136 
137  }
138   for(int k=no_of_bits-1; k>=(no_of_bits-X);k--)
139  {
140      result=result+(pow(2,power++)*A[k]);
141 
142  }
143 
144 
145 
146  }
147  else  /// if number is negative
148  {int product[X+Qsize],tempproduct[X+Qsize];
149  int prosize=X+Qsize,sum=0,carry=0;
150  cout<<" Answer is negative so Taking its Two's Complement..\n";
151 
152  for(int i=0;i<X+Qsize;i++) tempproduct[i]=0; temp[(X+Qsize)-1]=1;
153 
154  for(int k=0;k<Qsize;k++) product[--prosize]=Q[(no_of_bits-1)-k];
155  for(int k=0;k<X;k++) product[--prosize]=A[(no_of_bits-1)-k];
156 
157 
158  for(int i=(Qsize+X-1);i>=0;i--)
159      if(product[i]==0)product[i]=1;
160  else product[i]=0;
161  for(int i=(X+Qsize-1);i>=0;i--)
162      {sum=0;
163      sum=carry+product[i]+tempproduct[i];
164      if(sum==3)
165          {
166              sum=1;carry=1;
167          }else if(sum==2)
168          {
169              sum=0;carry=1;
170          }
171          else{
172              carry=0;
173          }
174          product[i]=sum;
175 
176      }
177  int power=0;
178  for(int i=(Qsize+X)-1;i>=0;i--)
179  {
180      result=result+(product[i]*pow(2,power++));
181  }
182 
183 
184  result=(result*-1)-1;
185  }
186 
187  cout<<"Result=="<<result<<endl;
188  out<<"Result=="<<result<<endl;
189 
190  out.close();
191  cout<<"\n";
192  pause;
193 
194 
195  }
196  void intialiser(int m ,int q)
197  {
198 
199  for(int i=0;i<no_of_bits;i++)
200  {
201      Q[i]=0;
202      A[i]=0;
203      temp[i]=0;
204      M[i]=0;
205      M2Complement[i]=0;
206 
207  }
208 
209 
210 
211  X=binary_convert(m,M);
212 
213  N=binary_convert(q,Q);
214  Qsize=N;
215  m=m*(-1);
216  X=binary_convert(m,M2Complement);
217 
218  out<<"\n==========================================================================\n";
219  out<<"\n M =\t";
220 
221  cout<<"\n==========================================================================\n";
222  cout<<"\n M =\t";
223  for(int i=no_of_bits-X;i<no_of_bits;i++){
224  cout<<M[i];out<<M[i];}
225  cout<<"\n Q =\t";
226  out<<"\n Q =\t";
227 
228  for(int i=no_of_bits-N;i<no_of_bits;i++){
229  cout<<Q[i];out<<Q[i];}
230  cout<<"\n A =\t";
231  out<<"\n A =\t";
232 
233  for(int i=no_of_bits-X;i<no_of_bits;i++){
234  cout<<A[i];out<<A[i];}
235  cout<<"\n-M =\t";
236  out<<"\n-M =\t";
237 
238  for(int i=no_of_bits-X;i<no_of_bits;i++)
239  {
240  cout<<M2Complement[i];out<<M2Complement[i];}cout<<"\n==========================================================================\n";
241 
242  out<<"\n==========================================================================\n";
243 
244  }
245 
246 
247 
248 
249 
250 
251 
252  void ARS()
253  {
254 
255  Q1=Q[no_of_bits-1];
256  for(int i=no_of_bits-1;i>0;i--)
257  {
258      Q[i]=Q[i-1];
259  }
260  for(int i=0;i<=(no_of_bits-Qsize);i++)
261  Q[i]=A[no_of_bits-1];
262  for(int i=no_of_bits-1;i>0;i--)
263  {
264      A[i]=A[i-1];
265  }
266 
267 
268 
269  N--; /// decreasing N value.
270  }
271 
272  int binary_convert(int num,int m[])
273  {bool ispositive=false;int counter=(no_of_bits-1),bitsize=0; /// counter will be used to set current bit value..
274 
275      if(num>0)
276      {
277          ispositive=true;
278      }
279 
280      if(ispositive==true) m[0]=0; /// setting signed bit ;;
281      else
282 
283      {
284          m[0]=1;
285      num=num*(-1);
286      }
287      while(num>0)
288      {bitsize++;
289          if(num%2==0) m[counter]=0;
290          else m[counter]=1; /// reminder ones....
291 
292  num=num/2;
293 
294  counter--;
295 
296      }
297      if(ispositive==false) /// i.e number is negative
298 
299  {
300          for(int counter=1;counter<no_of_bits;counter++)
301 
302          {
303              if(m[counter]==0) m[counter]=1;
304              else m[counter]=0;
305 
306          } /// taking One's Complement...
307 
308          for(int i=0;i<no_of_bits;i++)
309 
310          {if(i==(no_of_bits-1)) temp[i]=1;
311          else temp[i]=0;
312 
313 
314          } /// adding one...
315          Add(m,temp);
316 
317      }
318 
319  return (bitsize+1);
320  }
321  void Add(int FirstNumber[],int SecondNumber[] )
322  {int carry=0,sum=0;
323 
324  for(int i=(no_of_bits-1);i>=0;i--)
325  {sum=0;
326  sum=FirstNumber[i]+SecondNumber[i]+carry;
327 
328  if(sum==3)
329  {
330      sum=1;carry=1;
331  }else if(sum==2)
332  {
333 
334      sum=0; carry=1;
335  }
336  else
337  {
338      carry=0;
339  }
340 
341      FirstNumber[i]=sum;
342  }
343  }
get the source code





Comments

Popular posts from this blog

How to Protect an Email Account from being Hacked

If this is the case, then what is the reason for many people to lose their accounts? The answer is very simple. They don’t know how to protect themselves from being hacked! In fact most of the people who lose their email accounts are not the victims of hacking but the victims of Trapping. They lose their passwords not because they are hacked by some expert hackers but they are fooled to such an extent that they themselves give away their password. Are you confused? If so continue reading and you’ll come to know… Now I’ll mention some of the most commonly used online scams which fool people and make them lose their passwords. I’ll also mention how to protect your email account from these scams. 1 . WEBSITE SPOOFING =Website spoofing is the act of creating a website, with the intention of misleading the readers. The website will be created by a different person or organisation (Other than the original) especially for the purposes of cheating. Normally, the website...

Java API call Example using GSON, org.json.json and Jackson [ Simple Get Call] and parsing result as JSON

import com.fasterxml.jackson.databind.JsonNode ; import com.fasterxml.jackson.databind.ObjectMapper ; import com.google.gson.* ; import org.json.JSONArray ; import org.json.JSONObject ; import java.io.* ; import java.net.HttpURLConnection ; import java.net.URL ; public class APICALL { public static void main (String[] args) throws IOException { // String url="https://mocki.io/v1/19a50724-c2e5-46a1-b457-543462cdfde2"; String url= "https://jsonplaceholder.typicode.com/users" ; String line ; StringBuilder resp= new StringBuilder() ; System. out .println(url) ; HttpURLConnection con= (HttpURLConnection) new URL(url).openConnection() ; con.setRequestMethod( "GET" ) ; con.setRequestProperty( "Accept" , "application/json" ) ; System. out .println(con.getResponseMessage()) ; System. out .println(con.getContentType()) ; InputStream inputStream=con.getInput...

keyboard-shortcuts-that-work-in-all-web-browsers

Each major web browser shares a large number of keyboard shortcuts in common. Whether you’re using Mozilla Firefox, Google Chrome, Internet Explorer, Apple Safari, or Opera – these keyboard shortcuts will work in your browser. Each browser also has some of its own, browser-specific shortcuts, but learning the ones they have in common will serve you well as you switch between different browsers and computers. This list includes a few mouse actions, too. Tabs Ctrl+1-8 – Switch to the specified tab, counting from the left. Ctrl+9 – Switch to the last tab. Ctrl+Tab – Switch to the next tab – in other words, the tab on the right. (Ctrl+Page Up also works, but not in Internet Explorer.) Ctrl+Shift+Tab – Switch to the previous tab – in other words, the tab on the left. (Ctrl+Page Down also works, but not in Internet Explorer.) Ctrl+W, Ctrl+F4 – Close the current tab. Ctrl+Shift+T – Reopen the last closed tab. Ctrl+T – Open a new tab. Ctrl+N – Open a new browser window....

5 Best Popular Posts Widgets For Blogger

Adding the Popular Posts Widget for Blogger Just click on your blog title, access the "Layout" menu, click "Add a Gadget" and choose "Popular Posts". A window will appear asking you to configure the widget by choosing which posts you'll feature (e.g. those that were most viewed in the past 7 days or 30 days or from the beginning of your blog). You'll also be asked to choose how many posts you'll feature in your Popular Posts section and select if you'll show the post title only or along with the image thumbnail and/or the snippet. (Remember that each widget style has different requirements, so follow the styles and instructions carefully to find out if you need the snippet and image thumbnail or not). Popular Posts Style 1 - Box within a box This is an interesting widget style since it uses your snippet and image thumbnail in a unique way. Your snippet is written in opaque text and placed in a small transparent box. This, in turn, ...

Windows 10 1703 Fall Creator update/upgrage brings NEW UI ... the fluent Ui

Microsoft is planning to implement these subtle design changes gradually. Some are already available in new updates to existing Windows 10 apps, and more will start to appear in Windows itself as Microsoft updates the operating system with the Fall Creators Update and future updates. "It's going to be a journey," says Microsoft director Aaron Woodman, noting that these design changes will appear over time in Windows and other products. On stage at Build today, Microsoft's Joe Belfiore demonstrated a number of Fluent Design changes. "You're going to see Fluent Design show up in the Windows shell, in our apps, and across devices," explains Joe Belfiore. Microsoft is focusing on light, depth, motion, material, and scale for its Fluent Design, with subtle changes that make the design feel like it's moving during interactions in Windows. An inking demo showed how Microsoft is bringing the pen experience across the entirety of Windows, allowing...

DOWNLOAD SOFTWARES, GAMES , MUCH MORE BY BYPASS PAYPAL PAYMENT

Bypass Paypal Payment: Now Download, Software, Games. etc for free without paying any money. We all know that we need to pay via Paypal for download various things from Internet like Games, Ebooks, Software, etc. However this method works only few websites that have very weak security. So Today you are going to learn another method that is "Bypassing Paypal Payment". STEPS Just paste the below code in your address bar the site where the option come to pay Via Paypal CODE " javascript:top.location=document.getElementsByName('return')[0].value; javascript:void(0) " If this code worked out for you, you will be redirected to the download page, Elase bad luck, try another site SOME IMPORTANT TIPS This method is working only on those sites with weak security feature. Make sure that your browser supports JavaScript. one website on which this method are works you can try these sites which have weak security sites take it from fallowing link pastesite.com/22775

python program to Print Starting Series OF Indian Mobile Number for a State or operator or both

import requests import urllib.request import time from bs4 import BeautifulSoup as bs import re url = ' https://en.wikipedia.org/wiki/Mobile_telephone_numbering_in_India' state_to_extract = "UE" #if set to None all state is considered telecom_to_extracted = None #if set to none all operator from particular city is extracted response = requests . get(url) print (response) soup = bs(response . text, "html.parser" ) one_a_tag = soup . findAll( 'tr' )[ 35 :] lst = [] for k in one_a_tag: s = k . findAll( 'td' ) limit = len (s) i = 0 while True : if i == limit: break no = s[i] . text i += 1 if i == limit: break operator = s[i] . text i += 1 if i == limit: break state = s[i] . text i += 1 if i == limit: break res = f "{no} {operator} {state}" if state_to_extract is None : if telecom_to_extracted is None : lst . append(no) elif telecom_to_e...

Supported OS id and their code in an installer

Hello frnd this is the code and thier Operating system id in windows ,to identify which program run in which windows The ID below indicates application support for Windows Vista -->           --The ID below indicates application support for Windows 7 -->           -The ID below indicates application support for Windows 8 -       supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" code provided to you by Rajlive360.tk

Streamlining Java Web Application Deployment with React WAR Generator

In the ever-evolving world of web development, managing builds and deployments can often be cumbersome and error-prone. Today, we're excited to introduce a tool designed to simplify and streamline this process: the React WAR Generator . What is the React WAR Generator? The React WAR Generator is a Python-based tool that automates the creation of WAR (Web Application Archive) files for Java web applications. It caters specifically to frontend projects built with React or similar frameworks, making it easier to package and deploy your web applications to a Tomcat server. Key Features Profile-Based Builds : With support for multiple profiles ( dev , test , prod , default ), you can build your application according to different environments and configurations. Version File Generation : Optionally generate a version file that integrates versioning information directly into your TypeScript files, ensuring your build versions are always up-to-date. Tomcat Deployment : Simplify your deploy...