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

Random post with specific label - Blogger Widget

1. go to blogger dashboard, select template and edit html. 2. search for </head> to add script. - you may download and upload to your site, or just use this link (no download required) script <script src='http://docs.google.com/uc?id=0B7xJbTAja8i0a0ZJbXJ2TkkwSW8&amp;export=download' type='text/javascript'/> 2. search for ]]></b:skin> to add style/css. img.label_thumb{ float:left; padding:5px; border:1px solid #8f8f8f; background:#D2D0D0; margin-right:10px; height:55px; width:55px; } img.label_thumb:hover{ background:#f7f6f6; } .label_with_thumbs { float: left; width: 100%; min-height: 70px; margin: 0px 10px 2px 0px; adding: 0; } ul.label_with_thumbs li { padding:8px 0; min-height:65px; margin-bottom:10px; } .label_with_thumbs a {} .label_with_thumbs strong {} 2. save template. 3. add a widget. 3. edit widget. at this example i use random post with label "blogger", you must replace it with your label. <div s...

Submit your site for free to search engine~SEO TOOL SERACH ENGINE SUBMitt free

free Submit the main page of your site only. The rest of your site will be crawled by the search engines. Only 5 submissions in every 24 hours period are allowed. URL  * Email  * Name  * Business Phone  * Country  *   None Selected   United States   Afghanistan   Albania   Algeria   American Samoa   Andorra   Angola   Anguilla   Antigua and Barbuda   Argentina   Armenia   Aruba   Australia   Austria   Azerbaijan   Bahamas   Bahrain   Bangladesh   Barbados   Belarus   Belgium   Belize   Benin   Bermuda   Bhutan   Bolivia    Bosnia Hercegovina   Botswana   Bouvet Island   Brazil   Brunei Darussalam   Bulgaria   Burkina Faso   Burundi   Cambodia   Cameroon   Canada   Cape Verde   Cayman Islands   Central African Republic   Chad   Chi...

Tips to Enable or Disable Toast Notifications on Windows 8

Windows 8 is among the most incredible and interesting kind of Windows version from Microsoft. It has visually eye catching design, which is found in modern user interface that is meant for redesigning a number of operating systems, worked out for users. If you have used or seen someone using Windows 8 you could have noticed the toast notifications in this modern version of windows, which simply appears the moment you install or uninstall any application. There are many users who are well versed with the bubble notifications, which were found in the earlier versions of Windows seen via the taskbar. The new application platform in Windows 8 comes with an integrated notification system for installing or uninstalling a number of modern applications. The Windows 8 style application can employ a number of notifications types including the traditional toast notifications, live titles and lock screen. These can be managed by either disabling or enabling the notification of your applicatio...

List of all search engine by topics,genre and based on

Tweet General Baidu  (Chinese, Japanese) Bing Blekko Google Sogou  (Chinese) Soso.com  (Chinese) Volunia Yahoo! Yandex.com Yodao  (Chinese) P2P search engines FAROO Seeks  (Open Source) YaCy  (Free and fully decentralized) Metasearch engines See also:  Metasearch engine Blingo Yippy  (formerly Clusty) DeeperWeb Dogpile Excite Harvester42 HotBot Info.com Ixquick Kayak Mamma Metacrawler Mobissimo Otalo PCH Search and Win SideStep Thiv WebCrawler Geographically limited scope Accoona ,  China / United States Alleba , Philippines Ansearch ,  Australia / United States / United Kingdom / New Zealand Biglobe ,  Japan Daum ,  Korea Goo ,  Japan Guruji.com ,  India Leit.is ,  Iceland Maktoob ,  Arab World Miner.hu ,  Hungary Najdi.si ,  Slovenia Naver ,  Korea Onkosh ,  Arab World Rambler ,  Russia Rediff ,  India SAPO ,...

Downloading Windows 8 for Free [Full version]

Tweet Downloading Windows 8 for Free [Full version] Windows 8 is a new version of Windows that focuses on variety of hardware platform and form factors such as slim-type computers and new generation of touch devices. The new version free for download was made available for developers and testers of the new OS. Here’s the download links for the Enterprise version of Windows. How to get and download the latest operating system Thankfully, Microsoft released the new version free for download designed for technological professionals, engineers and IT personnel's for testing and debugging purposes. Download Links of the ISO file (x86 and x64) Windows 8 32-Bit version Windows 8 64-Bit version If you can’t download the file, make sure that you are login to your respected  Hotmail account .

Google Sheet/Google form Script to send automated Email to users

Well many of us want to send especially bloggers sometimes want to send automated replies to user 's ..but as usual, not everyone is a code geek or lovers ... so this is a small guide to How to use Google form with Google sheet to make an automated reply link....so follow the steps accordingly. STEP 1: GOTO google Forms ... and create a form ... in my case I just take users email id and how do they get to my site. 1. GOTO   https://docs.google.com/forms?usp=mkt_forms 2. login with your account. now choose blank form. 3. in Form title write your forms name, for example, let say my form. 4. in Form description write the description let say  A simple form ... 5. now go to setting and in general tab, check collect email address. and  click on save 6. (optional) you can also ask some basic question 7. now goto responses tab now click on create new spreadsheet button. (that green icon ..) in select response, destination cho...

Binary Search Tree in Java implementation (reference based, dynamic memory)

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 import java.util.Scanner ; class BST { static BST . Node root = null ; public void insert ( int num ) { if ( root == null ) { root = new BST . Node ( num ); } else { // root node is not empty BST . Node temp = root ; while ( temp != null ) { if ( num <= temp . getVal ()) { if ( temp . getLeft () != null ) temp = temp . getLeft (); ...

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

python program get union of two list (program to get A union B ) list method .

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 #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Fri Mar 16 17:08:52 2018 @author: beast """ def version1 (): a = [ 'a' , 'b' , 'c' , 'd' , 'e' ] # list 1 b = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' ] # list 2 c = [k for k in (a) if (k in (a) and k not in (b))] # include unique item from list 1 : items are (list1-list2)(set thoery) d = [l for l in (b) if l in (a ) and l in (b) or (l not in (a) and l in (b))] #include all the comman from list 1 and unique from list 2 lst = c + d # append above two comprehensed list to get union of list1 U list2 lst . sort() # not neccessay but makes list easy to understand (sorting in ascending order ) ...

5 Quick Beginner-Friendly CSS Customizations That Make Your Blog Stand Out

Changing Background Color To change the background of your website, you first have to get familiar with the styling of the theme. Is the background color simply under body, or is it built into its own frame? Some themes are not as intuitive as others, so if the one you are currently using is intelligible, you might want to change to a different theme before you start editing. (The one I'm using in the example is the free  Catchbox Theme  and a very common starting theme is twentyeleven.) With most non full-width themes, the background color is simply under body. And overruling it is quite simple. 1 2 3 body {    background-color : #477C67 ; } You can use the  W3Schools HTML color picker  to get your colors, or install a Chrome extension, aFirefox plugin, or a WordPress plugin to speed things along when you're editing on the go. I chose a deep teal background color:  #477C67 : If this code snippet does not change the ba...