मैं यहाँ आया था इस सवाल की तलाश में है और मैं 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/
क्या इसे मनमाने ढंग से पूर्णांक लेना है? यदि संचालन में से एक को दो की शक्ति होनी है तो गुणा को लागू करने का एक आसान तरीका है। साथ ही, क्या यह एक होमवर्क असाइनमेंट है या क्या आप एक प्रोसेसर पर एक प्रोसेसर पर असेंबली में गुणा को लागू करने की कोशिश कर रहे हैं जिसमें गुणा (या दोनों) शामिल नहीं हैं? –
दो कार्यान्वयन की शक्ति आसान है, लेकिन इस मामले में पूर्णांक दो की शक्ति नहीं हैं, वे मनमानी हैं। और यह होमवर्क सवाल नहीं है, यह एक साक्षात्कार सवाल है। कृपया संलग्न कार्यान्वयन की जांच करें। – SuperMan
प्रश्न में लिंक मौजूद नहीं है –