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

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

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

How to Bypass Right Click Block on Any Website

In order to block the right-click activity, most websites make use of JavaScript which is one of the popular scripting languages used to enhance functionality, improve user experience and provide rich interactive features. In addition to this, it can also be used to strengthen the website’s security by adding some of the simple security features such as  disabling right-click ,  protecting images ,  hiding or masking parts of a web page  and so on. How JavaScript Works? Before you proceed to the next part which tells you how to disable the JavaScript functionality and bypass any of the restrictions imposed by it, it would be worthwhile for you to take up a minute to understand how JavaScript works. JavaScript is a client side scripting language (in most cases), which means when loaded it runs from your own web browser. Most modern browsers including IE, Firefox, Chrome and others support JavaScript so that they can interpret the code and carry out action...

DOWNLOAD CODE BLOCKS 16.01 MINGW.SETUP .EXE 86.3 MB

Code::Blocks for Mac is a free C, C++ and Fortran IDE that has a custom build system and optional Make support. The application has been designed to be very extensible and fully configurable. Code::Blocks is an IDE packed full of all the features you will need. It has a consistent look, feel and operation across its supported platforms. It has been built around a plugin framework, therefore Code::Blocks can be extended with plugins. Support for any kind of functionality can be added by installing/coding a plugin. Key features include: Written in C++. No interpreted languages or proprietary libs needed.. Full plugin support. Multiple compiler support: GCC (MingW / GNU GCC), MSVC++, clang, Digital Mars, Borland C++ 5.5, and Open Watcom etc. Support for parallel builds. Imports Dev-C++ projects. Debugger with full breakpoints support. Cross-platform. Code::Blocks' interface is both customizable and extensible with Syntax highlighting, a tabbed interface, Class Br...

FIXED: Feedjit gadgets no longer work on my blog!!! help

well if your feedjit tracker is not working than you can easily fix this... goto blogger.com Go to  Dashboard -> Settings And set  HTTPS Redirect  to  No The gadgets will work now for  http ://yourblog.blogspot.com They won't for  https ://yourblog.blogspot.com because their connection isn't secure. this is not a permanent fix ... the gadget developers will have to make their gadgets https ready before Blogger will (if at all) become https only.

Binary Search Tree in C++( dynamic memory based )

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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 #include<bits/stdc++.h> using namespace std; struct bst { int val; bst * left, * right; }; bst * root = nullptr; void srch ( int num,bst * head) { if (head == nullptr){ cout << " \n Number is not present \a " << endl; return ; } if (head -> val == num) { cout << " \n Number is present \n\a " ; return ; } else { if (num < head -> val) srch(num,head -> left); else srch(num,head -> right); ...

Creating an Executable Jar File

Creating a jar File in  Eclipse In  Eclipse  Help contents, expand "Java development user guide" ==> "Tasks" ==> "Creating JAR files."  Follow the instructions for "Creating a new JAR file" or "Creating a new runnable JAR file."The  JAR File  and  Runnable JAR File  commands are for some reason located under the  File menu: click on  Export...  and expand the  Java  node. Creating a jar File in  JCreator You can configure a "tool" that will automate the jar creation process.  You only need to do it once. Click on  Configure/Options . Click on  Tools  in the left column. Click  New , and choose  Create Jar file . Click on the newly created entry  Create Jar File  in the left column under  Tools . Edit the middle line labeled  Arguments:  it should have cvfm $[PrjName].jar manifest.txt *.class Click OK. Now set...

How to install gta sanandreas mods

INdex 1.GTA San Andreas System Requirements 2.Backing Up 3.Installing The Mod --Method 1: Using IMG Tool --Method 2 Self Mod Installers ----GMM ----San Andreas Mod Installer --Installing cloths --Installing Weapons 4.Tips 5.My Mods arn't working! Starting off 1.GTA San Andreas System Requirements You need to make sure you computer can run San Andrease. Theese are the Requirements. Recommended System Requirements: Intel Pentuim 4 or AMD XP Processor (or better) 384MB RAM 16x Speed DVD Drive 4.7GB Hard Drive Space 128MB Video Card DirectX 9 compatible sound & video drivers Keyboard, mouse or game pad Minimum System Requirements: 1GHz Pentuim III or AMD Athlon 256MB RAM 8x Speed DVD Drive 3.6 GB Hard Disk Space 64MB Video Card DirectX 9 compatable sound & video drivers Keyboard, mouse 2.Backing Up Before you do anything to the game, back up the files you are going to mod. I usally copy the GTA folder and rename it "Normal San And...