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

13 websites to register your free domain

Register your Free Domain Now!! 1)  .tk Dot TK is a FREE domain registry for websites on the Internet. It has exactly the same power as other domain extensions, but it’s free! Because it’s free, millions of others have been using .TK domains since 2001 – which makes .TK powerful and very recognizable.  Your website will be like www.yourdomainname.tk . It is free for 1 year. It’s a ccTLD domain whixh having the abbreviation  Tokelau. To create a .tk domain, Visit   www.dot.tk 2) co.cc Co.cc is completely free domain which is mostly used by blogspot bloggers because of it’s easy to use DNS system. Creating a co.cc for blogger is simple ( for instructions- “click here”). Your website will be like www.yourdomainname.co.cc . To create a .co.cc domain, visit www.co.cc 3)   co.nr co.nr is too like co.cc. Your website will be like  www.yourdomainname.co.nr . You can add it for blogger also.. To create a .co.cc domain, vi...

what is LOREM ipsum and why do designers use it

What is Lorem Ipsum? Lorem Ipsum  is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now...

download Code blocks 13.12 mingw.setup .exe 97 mb

NOTE: A newer version is been updated on the site ... visit here  http://vastgk.blogspot.com/2017/07/download-code-blocks-1601-mingwsetup.html File Date Download from codeblocks-13.12-setup.exe 27 Dec 2013 BerliOS  or  Sourceforge.net codeblocks-13.12mingw-setup.exe 27 Dec 2013 BerliOS  or  Sourceforge.net codeblocks-13.12mingw-setup-TDM-GCC-481.exe 27 Dec 2013 BerliOS  or  Sourceforge.net NOTE : The codeblocks-13.12mingw-setup.exe file  includes  the GCC compiler and GDB debugger from  TDM-GCC  (version 4.7.1, 32 bit). The codeblocks-13.12mingw-setup-TDM-GCC-481.exe file includes the TDM-GCC compiler, version 4.8.1, 32 bit. While v4.7.1 is rock-solid (we use it to compile C::B), v4.8.1 is provided for convenience, there are some known bugs with this version related to the compilation of Code::Blocks itself. IF UNSURE, USE "codeblocks-13.12mingw-setup.exe"!  \ Linux 32-bit: Dis...

wanted to know your ip address

  there are two ways  1. Goto this site at bottom there u will see your ip address and your network name 2. goto Start >control PAnel>Netwoek connections  {in win xp and in classic view } 3. Goto your connection name 4. now in left side in Details u will see ur ip address now only means when u r online

C++ Program to implement File Handling With Class Objects

Q32. Program to implement File Handling With Class Objects: Define a class to represent a bank account. Include the following members:   i) Depositor Name  ii) Account Number iii) Balance Amount Member Function   i) To Assign Initial values(opening balance Rs. 1000 by default)  ii) To deposit an amount iii) To withdraw an amount(if possible) iv) To display the current balance Write a file based program to store the records of at least ten accounts in “ACCOUNT_DETAIL” data file and perform the required manipulations- DEPOSIT, WITHDRAW & BAL_ENQUIRY using the file on a given account. The changes after each deposit and withdrawal should be updated in the given file. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 7...

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...

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...

just more way to disable autorun.inf

Auto run.Inf this is a instruction file associated with the Auto run function. It is a simple text configuration file that instructs the OS (operating system) which executable to start which icon to use which additional menu commands to make available etc Auto run.inf must be located in the root directory of a volume.That is CD,DVD,of Floppy Disk or Pen drive. It is mainly used by the manufacturer on what actions to taken when their CD-ROM when it is inserted. In OS, when autorun.inf is enabled (Normally by default it is enabled ) then by inserting the Cd or DVD the content of the medium is automatically executed. This is to avoid the user intervention and help the low level knowledge of computer literacy people. But Virus programmer taken this as advantage and make virus instruction in autorun.inf text file. TYPICAL AUTORUN.INF A typical autorun.inf file looks like below. [autorun] open=setup.exe icon=setup.exe,0 label=GameProgram SIMPLE METHOD NOT TO GET INFECTED BY AUTORUN.INF:...

Samsung mobile cheat codes

* Software version: *#9999# * IMEI number: *#06# * Serial number: *#0001# * Battery status- Memory capacity : *#9998*246# * Debug screen: *#9998*324# – *#8999*324# * LCD kontrast: *#9998*523# * Vibration test: *#9998*842# – *#8999*842# * Alarm beeper – Ringtone test : *#9998*289# – *#8999*289# * Smiley: *#9125# * Software version: *#0837# * Display contrast: *#0523# – *#8999*523# * Battery info: *#0228# or *#8999*228# * Display storage capacity: *#8999*636# * Display SIM card information: *#8999*778# * Show date and alarm clock: *#8999*782# * The display during warning: *#8999*786# * Samsung hardware version: *#8999*837# * Show network information: *#8999*638# * Display received channel number and received intensity: *#8999*9266# * *#1111# S/W Version * *#1234# Firmware Version * *#2222# H/W Version * *#8999*8376263# All Versions Together * *#8999*8378# Test Menu * *#4777*8665# GPSR Tool * *#8999*523# LCD Brightness * *#8999*377# Error LOG Menu *...