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

QWERTY-keyboard when this idean came

QWERTY QWERTY   / ˈ k w Éœr t i /  is the most common modern-day  keyboard layout . The name comes from the first six  keys  appearing on the top left letter row of the keyboard and read from left to right: Q-W-E-R-T-Y. The QWERTY design is based on a layout created for the  Sholes and Glidden typewriter  and sold to  Remington  in 1873. It became popular with the success of the Remington No. 2 of 1878, and remains in use on electronic keyboards due to the  network effect  of a standard layout and a belief that  alternatives  fail to provide very significant advantages. [ 1 ]  The use and adoption of the QWERTY keyboard is often viewed as one of the most important case studies in  open standards  because of the widespread, collective adoption and use of the product. [ 2 ] History and purposes [ edit ] Keys are arranged on diagonal columns, to give space for the levers. Main article:  Sholes and Glidden typewriter Still used to this day, the QWERTY layout was devised

Dragon Age: Inquisition Digital Deluxe Edition + All DLCs (torrent) Repack Size: 20.1~23.9 GB

Brief : Dragon Age: Inquisition  is an  action role-playing video game  developed by  Bioware Edmonton  and published by  Electronic Arts . The third major game in the  Dragon Age  franchise,  Dragon Age: Inquisition  is the sequel to  Dragon Age: Origins  and  Dragon Age II . The game was released worldwide in November 2014 for  Microsoft Windows ,  PlayStation 3 ,  PlayStation 4 ,  Xbox 360 , and  Xbox One . Repack Size: 20.1~23.9 GB 

How to Create Your Own Customized Run Commands

The Run command on Microsoft Windows operating system allows you to directly open an application or document with just a single command instead of navigating to it’s location and double-clicking the executable icon. However, it only works for some of the inbuilt Windows programs such as Command prompt (cmd), Calculator (calc) etc. So, have you ever wondered how to create your own customized Run commands for accessing your favorite programs, files and folders? Well, read on to find out the answer. Creating the Customized Run Command: Let me take up an example of how to create a customized run command for opening the Internet explorer. Once you create this command, you should be able to open the Internet explorer just by typing ie in the Run dialog box. Here is how you can do that. Right-click on your Desktop and select New -> Shortcut. You will see a “Create Shortcut” Dialog box as shown below Click on “Browse”, navigate to: Program Files -> Internet Explorer from y

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

robot.txt what is this and how to use this

What do they do exactly? Robot.txt files tell your instructions to a search engine robot.. The first thing a search engine spider looks at when it is visiting a page is the robots.txt file. It looks for it because it wants to know what it should do. If you have instructions for a search engine robot, you must tell it those instructions. The most common problem people have with robot.txt files is that they don't know how to make them. If you can make web pages, you can also make a robot.txt file. The file is a text file, which means that you can use notepad, wordpad, or any other plain text editor. You can also make them in Frontpage or Dreamweaver by using the "code" view. You can even "copy and paste" them. So instead of thinking "I am making a robot.txt file", just think, "I am writing a note" they are the exact same process. However you would write a note or a letter on your computer will work for the robot.txt file. What should

How to open facebook through Uninor when it is blocked

Tweet Just Follow this steps . 1. Goto www.uninor.in 2. choose your circle 3.Goto  http://www.uninor.in/customer-care/Pages/customer-complaint-log.aspx 4. Now in new complaint just type your moblie number 5.now you will receive passwrod just type in and hit submit 7. NOW select as following as type as it in every box Complaint Type   -- Select --   DND Related   GPRS   Network Related   Tariff & VAS Related   Activation/ Deactivation   Barring/ Unbarring   Balance Related    Complaint Area   -- Select --   Activation / Deactivation   PC/Modem Connectivity   Usage / Charges related   GPRS issues   Non-Uninor sites   Complaint sub Area   -- Select --   Unable to browse   Unable to download   Please ensure all questions below are answered in the text box below. 1. Handset make and model no. 2. Web Site Name/ URL 3. Error message

Overview of C

Why use C?: C has been used successfully for every type of programming problem imaginable from operating systems to spreadsheets to expert systems - and efficient compilers are available for machines ranging in power from the Apple Macintosh to the Cray supercomputers. The largest measure of C's success seems to be based on purely practical considerations: the portability of the compiler; the standard library concept; a powerful and varied repertoire of operators; an elegant syntax; ready access to the hardware when needed; and the ease with which applications can be optimized by hand-coding isolated procedures C is often called a "Middle Level" programming language. This is not a reflection on its lack of programming power but more a reflection on its capability to access the system's low level functions. Most high-level languages (e.g. Fortran) provides everything the programmer might want to do already built into the language. A low l

Download pocket tank delux with 295 weapons free total 295 weapons version 1.6

Download Pocket Tanks Deluxe Full Version Free With 295 Weapons Pack | Size: 30MB UPDATED 2019 /19/april Description: Pocket Tanks is a 1-2 player computer game for Windows and Mac OS X, created by Blitwise Productions, developer of Super DX-Ball and Neon Wars. Adapted from Michael Welch's earlier Amiga game Scorched Tanks, this newer version features modified physics, dozens of weapons ranging from simple explosive shells to homing missiles, and the ability to move the tank. It supports several expansion packs. At the moment, players can have up to 295 different weapons total. Pocket Tanks is often abbreviated as PTanks. Have Fun! NOTE: FILE NAME IS SCRAMBLED FOR AVOIDING HARD DETECTION & FILE TAKEN DOWN . How to Play: Best with 2 players on the same computer at school or at work. UPDATED LINK https://mirr.re/d/u1Y https://nl26.seedr.cc/ff_get/447027537/ptd16.295.exe?st=lUp-PbRp4YOwToHIOGwStQ&e=1555747979 http://www.uploadmagnet.com/7gfzhbyfe

Error:(1, 0) Plugin with id 'com.android.application' not found

just add the following code to your top level build.gradle  {note by top level build.gradle i means that if you open that build.gradle you will see a comment like this // Top-level build file where you can add configuration options common to all sub-projects/modules. add the following code  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:0.12.+' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! and click on sync now ..it will do the rest..