2010-12-16 19 views
25

मैं बिटवाई ऑपरेटरों का उपयोग करके दो पूर्णांक कैसे बढ़ा सकता हूं?बिटवाई ऑपरेटरों का उपयोग करके दो पूर्णांक का गुणा

मुझे एक कार्यान्वयन here मिला। क्या गुणा को लागू करने का एक बेहतर तरीका है?

उदाहरण के लिए: 2 * 6 = 12 बिटवाई ऑपरेटरों का उपयोग करके किया जाना चाहिए।

नोट: नंबर, मनमाने ढंग से कर रहे हैं 2

+0

क्या इसे मनमाने ढंग से पूर्णांक लेना है? यदि संचालन में से एक को दो की शक्ति होनी है तो गुणा को लागू करने का एक आसान तरीका है। साथ ही, क्या यह एक होमवर्क असाइनमेंट है या क्या आप एक प्रोसेसर पर एक प्रोसेसर पर असेंबली में गुणा को लागू करने की कोशिश कर रहे हैं जिसमें गुणा (या दोनों) शामिल नहीं हैं? –

+0

दो कार्यान्वयन की शक्ति आसान है, लेकिन इस मामले में पूर्णांक दो की शक्ति नहीं हैं, वे मनमानी हैं। और यह होमवर्क सवाल नहीं है, यह एक साक्षात्कार सवाल है। कृपया संलग्न कार्यान्वयन की जांच करें। – SuperMan

+0

प्रश्न में लिंक मौजूद नहीं है –

उत्तर

41
#include<stdio.h> 

main() 
{ 
    int a, b, result; 
    printf("\nEnter the numbers to be multiplied:"); 
    scanf("%d%d", &a, &b);  // a > b 
    result = 0; 
    while (b != 0)    // Iterate the loop till b == 0 
    { 
     if (b & 01)    // Bitwise & of the value of b with 01 
     { 
      result = result + a; // Add a to result if b is odd . 
     } 
     a<<=1;     // Left shifting the value contained in 'a' by 1 
            // Multiplies a by 2 for each loop 
     b>>=1;     // Right shifting the value contained in 'b' by 1. 
    } 
    printf("nResult:%d",result); 
} 

Source

+0

क्या आप अपना कोड समझा सकते हैं? –

+1

@ वेक्टर 9 क्या आपके पास एक विशिष्ट चीज है जिसे आपको समझने की आवश्यकता है? तर्क का दिल एक मूल्य को 2 से गुणा कर रहा है और जब भी अन्य मान अजीब होता है तो इसे परिणाम में जोड़ना।यह 2 इनपुट गुणा (बाएं)/विभाजित (दाएं) गुणा करने के लिए थोड़ा स्थानांतरण का उपयोग किया। अंकगणितीय ऑपरेटरों के संदर्भ में यह सोच रहा है: 'जबकि (बी! = 0): अगर (बी अजीब है) {परिणाम = परिणाम + ए;} ए = ए * 2; बी = बी/2; '। एक साधारण गणितीय चाल। @ शिव की चाल भी नीदर है जहां उसने 'परिणाम = परिणाम + ए' – zengr

+0

से बचा है हां मैंने समझ लिया है कि आप कर रहे हैं: ए * बी * (2/2) -> (ए * 2) * (बी/2)। जो मुझे नहीं मिला वह परिणाम = परिणाम + का उपयोग अजीब होने पर किया जाता है। –

4

bitwise operator applications पर विकिपीडिया प्रविष्टि कुछ छद्म कोड है की शक्ति नहीं है, लेकिन यह इसके अलावा ऑपरेटर के साथ-साथ बिटवाइज़ ऑपरेटर्स का उपयोग करता है।

2

विधानसभा एल्गोरिथ्म: इस तथ्य यह है कि कुल्हाड़ी * 7 = (कुल्हाड़ी * 8) -ax से सीधे इस प्रकार है।

mov  bx, ax   ;Save AX*1 
shl  ax, 1   ;AX := AX*2 
shl  ax, 1   ;AX := AX*4 
shl  ax, 1   ;AX := AX*8 
sub  ax, bx   ;AX := AX*7 

हर पारी चरण 2

1

द्वारा एक गुणा सी # में है यहां समारोह के कार्यान्वयन है।

public static int Mul(int a, int b) 
    { 
     int r = 0; 
     while (b != 0) 
     { 
      var temp = b & 1; 

      if (temp!= 0) 
      { 
       r = r + a; 
      } 
      a= a << 1; 
      b= b >> 1; 
      if (temp == 0) 
      { 
       r = a; 
       break; 
      } 
     } 

     return r; 
    } 
+1

इस उत्तर में एक गलती है। मैंने एक संपादन प्रस्तुत किया, गलती को हटाने के लिए एक मॉडरेटर से पूछा, और यह उनकी प्रतिक्रिया थी: _ जवाब जो कोड में कोड बदलते हैं, लगभग हमेशा अस्वीकार कर दिए जाते हैं। आपको या तो उत्तर के तहत कोई टिप्पणी छोड़नी होगी, या अपने स्वयं के एक नए उत्तर की आवश्यकता होगी ._ उपर्युक्त कोड में गलती 'if (temp == 0)' है। ब्रेक समेत संपूर्ण अगर कथन हटा दिया जाना चाहिए। इसे देखने के लिए, फ़ंक्शन को 1 और 4 के इनपुट के साथ कॉल करें: Mul (1, 4), और वापसी मान 2 होगा। स्पष्ट रूप से 1 x 4 नहीं है 2. – Barzee

+0

यह कोड गलतियों को निर्देशित करता है। और आपने पोस्ट किए गए कोड (स्वीकृत उत्तर) की जांच क्यों नहीं की? –

5

यह पूरी तरह से बिट-वार ऑपरेशंस के साथ है।

public int bitwiseMultiply(int a, int b) { 
    if (a ==0 || b == 0) { 
     return 0; 
    } 

    if (a == 1) { 
     return b; 
    } 
    else 
     if (b == 1) { 
      return a; 
     } 


    int result = 0; // Not needed, just for test 
    int initA = a; 
    boolean isORNeeded = false; 
    while (b != 0) { 

     if (b == 1) { 
      break; 
     } 

     if ((b & 1) == 1) { // Carry needed, odd number 
      result += initA; // Test, not needed 
      isORNeeded = true; 
     } 

     a <<= 1; // Double the a 
     b >>= 1; // Half the b 
     System.out.println("a=["+a+"], b=["+b+"], result=["+result+"]"); 
    } 

    return (isORNeeded ? (a | initA) : a); // a + result; 
} 
0
#include<stdio.h> 
    void main() 
    { 
     int n, m, i, j, x, large, small, t1, m2, result, a1, a2, a3, c, c1, c2, r, r1, la, re; 

     printf("Enter two numbers\n"); 
     scanf("%d%d", &n, &m); 
     result = 0; 

     if (n > m) 
     { 
      large = n; 
      small = m; 
     } 
     else 
     { 
      large = m; 
      small = n; 
     } 

     c = 0; 

     while (small) 
     { 
      t1 = small; 
      t1 &= 1; 

      if (t1 == 1) 
      { 
       printf("\n %d", large); 
       la = large; 
       re = result; 
       m2 = 0; 
       r1 = 1; 
       while (re || la || c) 
       { 
        a2 = la; 
        a2 &= 1; 
        a3 = re; 
        a3 &= 1; 

        c1 = a2 & a3; 
        r = a3^a2; 

        c2 =r & c; 
        r ^= c; 
        if (c1 || c2) 
         c = 1; 
        else 
         c = 0; 

        result &= ~r1; 
        x = r; 
        m2 >>= 1; 
        while (m2) 
        { 
         r <<= 1; 
         m2 >>= 1; 
        } 

        result |= r; 
        la >>= 1; 
        re >>= 1; 
        r1 <<= 1; 
        m2 = r1; 
       } 

      } 
      large <<= 1; 
      small >>= 1; 

     } 
     printf("\n%dX%d= %d\n", n, m, result); 
    } 
11

मैं यहाँ आया था इस सवाल की तलाश में है और मैं Zengr के जवाब सही लगता है। धन्यवाद ज़ेंगर! लेकिन एक संशोधन है जिसे मैं देखना चाहता हूं कि उसके कोड में '+' ऑपरेटर से छुटकारा पा रहा है। यह किसी भी एथिथमेटिक ऑपरेटर का उपयोग करके दो मनमानी संख्याओं के गुणा को कम करना चाहिए लेकिन सभी bitwise।

Zengr का पहला समाधान:

#include<stdio.h> 
main() 
{ 
    int a,b,result;  
    printf("nEnter the numbers to be multiplied :"); 
    scanf("%d%d",&a,&b);   // a>b 
    result=0; 
    while(b != 0)    // Iterate the loop till b==0 
    { 
     if (b&01)    // Bitwise & of the value of b with 01 
     { 
      result=result+a;  // Add a to result if b is odd . 
     } 
     a<<=1;     // Left shifting the value contained in 'a' by 1 
          // multiplies a by 2 for each loop 
     b>>=1;     // Right shifting the value contained in 'b' by 1. 
    } 
    printf("nResult:%d",result); 
} 

मेरे जवाब होगा: के रूप में जोड़ने

int Add(int x, int y) 
{ 
    // Iterate till there is no carry 
    while (y != 0) 
    { 
     // carry now contains common set bits of x and y 
     int carry = x & y; 

     // Sum of bits of x and y where at least one of the bits is not set 
     x = x^y; 

     // Carry is shifted by one so that adding it to x gives the required sum 
     y = carry << 1; 
    } 
    return x; 
} 

या रिकर्सिवली:

#include<stdio.h> 
main() 
{ 
    int a,b,result;  
    printf("nEnter the numbers to be multiplied :"); 
    scanf("%d%d",&a,&b);   // a>b 
    result=0; 
    while(b != 0)    // Iterate the loop till b==0 
    { 
     if (b&01)    // Bitwise & of the value of b with 01 
     { 
      result=add(result,a);  // Add a to result if b is odd . 
     } 
     a<<=1;     // Left shifting the value contained in 'a' by 1 
           // multiplies a by 2 for each loop 
     b>>=1;     // Right shifting the value contained in 'b' by 1. 
    } 
    printf("nResult:%d",result); 
} 

मैं कहाँ लिखा है के रूप में (जोड़ने)

int Add(int x, int y) 
{ 
    if (y == 0) 
     return x; 
    else 
     return Add(x^y, (x & y) << 1); 
} 

अतिरिक्त कोड के लिए स्रोत: http://www.geeksforgeeks.org/add-two-numbers-without-using-arithmetic-operators/

संबंधित मुद्दे