Computer >> कंप्यूटर >  >> प्रोग्रामिंग >> C++

C++ प्रोग्राम 2 हस्ताक्षरित संख्याओं के गुणन के लिए बूथ के गुणन एल्गोरिथम को लागू करने के लिए

बूथ का एल्गोरिथ्म एक गुणन एल्गोरिथ्म है जो दो हस्ताक्षरित बाइनरी नंबरों को 2 के कॉम्प्लिमेंट नोटेशन में गुणा करता है। बूथ ने डेस्क कैलकुलेटर का इस्तेमाल किया जो जोड़ने की तुलना में शिफ्टिंग में तेज़ थे और उन्होंने अपनी गति बढ़ाने के लिए एल्गोरिदम बनाया।

एल्गोरिदम

Begin
   Put multiplicand in BR and multiplier in QR
      and then the algorithm works as per the following conditions:
   1. If Qn and Qn+1 are same i.e. 00 or 11 perform arithmetic shift by 1 bit.
   2. If Qn Qn+1 = 10 do A= A + BR and perform arithmetic shift by 1 bit.
   3. If Qn Qn+1 = 01 do A= A – BR and perform arithmetic shift by 1 bit.
End

उदाहरण कोड

#include<iostream>
using namespace std;
void add(int a[], int x[], int q);
void complement(int a[], int n) {
   int i;
   int x[8] = { NULL };
   x[0] = 1;
   for (i = 0; i < n; i++) {
      a[i] = (a[i] + 1) % 2;
   }
   add(a, x, n);
}
void add(int ac[], int x[], int q) {
   int i, c = 0;
   for (i = 0; i < q; i++) {
      ac[i] = ac[i] + x[i] + c;
      if (ac[i] > 1) {
         ac[i] = ac[i] % 2;
         c = 1;
      }else
         c = 0;
      }
   }
   void ashr(int ac[], int qr[], int &qn, int q) {
      int temp, i;
      temp = ac[0];
      qn = qr[0];
      cout << "\t\tashr\t\t";
      for (i = 0; i < q - 1; i++) {
         ac[i] = ac[i + 1];
         qr[i] = qr[i + 1];
      }
      qr[q - 1] = temp;
   }
   void display(int ac[], int qr[], int qrn) {
      int i;
      for (i = qrn - 1; i >= 0; i--)
         cout << ac[i];
      cout << " ";
      for (i = qrn - 1; i >= 0; i--)
         cout << qr[i];
   }
   int main(int argc, char **argv) {
      int mt[10], br[10], qr[10], sc, ac[10] = { 0 };
      int brn, qrn, i, qn, temp;
      cout << "\n--Enter the multiplicand and multipier in signed 2's
      complement form if negative--";
      cout << "\n Number of multiplicand bit=";
      cin >> brn;
      cout << "\nmultiplicand=";
      for (i = brn - 1; i >= 0; i--)
         cin >> br[i]; //multiplicand
      for (i = brn - 1; i >= 0; i--)
         mt[i] = br[i];
      complement(mt, brn);
      cout << "\nNo. of multiplier bit=";
      cin >> qrn;
      sc = qrn;
      cout << "Multiplier=";
      for (i = qrn - 1; i >= 0; i--)
         cin >> qr[i];
         qn = 0;
         temp = 0;
         cout << "qn\tq[n+1]\t\tBR\t\tAC\tQR\t\tsc\n";
         cout << "\t\t\tinitial\t\t";
         display(ac, qr, qrn);
         cout << "\t\t" << sc << "\n";
         while (sc != 0) {
            cout << qr[0] << "\t" << qn;
            if ((qn + qr[0]) == 1) {
               if (temp == 0) {
                  add(ac, mt, qrn);
                  cout << "\t\tsubtracting BR\t";
                  for (i = qrn - 1; i >= 0; i--)
                     cout << ac[i];
                  temp = 1;
               }
            else if (temp == 1) {
               add(ac, br, qrn);
               cout << "\t\tadding BR\t";
               for (i = qrn - 1; i >= 0; i--)
                  cout << ac[i];
                  temp = 0;
            }
            cout << "\n\t";
            ashr(ac, qr, qn, qrn);
         }
         else if (qn - qr[0] == 0)
            ashr(ac, qr, qn, qrn);
            display(ac, qr, qrn);
            cout << "\t";
            sc--;
            cout << "\t" << sc << "\n";
   }
   cout << "Result=";
   display(ac, qr, qrn);
}

आउटपुट

--Enter the multiplicand and multipier in signed 2's complement form if
negative--
Number of multiplicand bit=5
multiplicand=0 1 1 1 1
No. of multiplier bit=5
Multiplier=1 0 1 1 1
qn q[n+1] BR AC QR sc
initial 00000 10111 5
1 0 subtracting BR 10001
ashr 11000 11011 4
1 1 ashr 11100 01101 3
1 1 ashr 11110 00110 2
0 1 adding BR 01101
ashr 00110 10011 1
1 0 subtracting BR 10111
ashr 11011 11001 0
Result=11011 11001

  1. C++ प्रोग्राम क्रैश होने के कारण

    C++ प्रोग्राम के असामान्य व्यवहार से अक्सर प्रोग्राम क्रैश हो जाता है। आपको सेगमेंटेशन फॉल्ट, एबॉर्टेड, फ़्लोटिंग पॉइंट अपवाद आदि जैसी समस्याओं का सामना करना पड़ सकता है। निम्नलिखित नमूना प्रोग्राम हैं जो आपको C++ प्रोग्राम क्रैश के कारणों को समझने में मदद कर सकते हैं। अपवाद सी ++ में अपवाद किसी प्

  1. Collatz अनुमान को लागू करने के लिए C++ प्रोग्राम

    इस ट्यूटोरियल में, हम Collatz Conjecture को लागू करने के लिए एक प्रोग्राम पर चर्चा करेंगे। इसके लिए हमें एक संख्या n दी जाएगी और हमें यह पता लगाना होगा कि क्या इसे दो संक्रियाओं का उपयोग करके 1 में बदला जा सकता है - यदि n सम है, तो n को n/2 में बदल दिया जाता है। अगर n विषम है, n को 3*n + 1 मे

  1. सरणी तत्वों के गुणन के लिए C++ प्रोग्राम

    पूर्णांक तत्वों की एक सरणी के साथ दिया गया और कार्य एक सरणी के तत्वों को गुणा करना और इसे प्रदर्शित करना है। उदाहरण Input-: arr[]={1,2,3,4,5,6,7} Output-: 1 x 2 x 3 x 4 x 5 x 6 x 7 = 5040 Input-: arr[]={3, 4,6, 2, 7, 8, 4} Output-: 3 x 4 x 6 x 2 x 7 x 8 x 4 = 32256 नीचे दिए गए कार्यक्रम में उपयोग क