Thursday, July 17, 2008

Adding two natural numbers using logical operators & without using arithmetic and unary operators

how to add two numbers without using arithmetic and unary operators

 

Best Answer was from Sri

 

#include <stdio.h>
      int add(int a, int b)
      {
     if (!a)
  return b;
     else
  return add((a & b) << 1, a ^ b);
      }

 

      int main()
      {
     unsigned int a,b;
  printf("Enter the two numbers: \n");

 

     scanf("%d",&a);
     scanf("%d",&b);
     printf("Sum is: %d",add(a,b));
      }

 

Regards,

Sri

>>>>>>>>>>>>>>>>>>>> 

This looks like is a standard answer for this question  - available in INTERNET

>>>>>>>>>>>>>>>>>>>> 

KIRAN  (sum= s[n]*pow(2,n) + sum;) – this uses + right???

>>>>>>>>>>>>> 

#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

{

 int n;

 int a[10], b[10], c[10],s[10];

 int x ,y ,sum=0;

 c[0]= 0;

 

 printf("enter any two numbers below 256 \n");  scanf("%d%d",&x,&y);

 

    for(n=0;n<=9;n++)

 {

   a[n]= (x>>n)&1;

   b[n]= (y>>n)&1;

   s[n] = ((a[n])^(b[n]))^c[n];

      c[n+1] = (a[n] & b[n])|(b[n] & c[n])|(c[n] & a[n]);  }

  for(n=9;n>=0;n--)

  {

  printf("%d\t",s[n]);

  sum= s[n]*pow(2,n) + sum;

  }

printf("\nsum = %d\n", sum);

}

 

thanks

kiran k

 

VIDYASAGAR

>>>>>>>>>>> 

 

#include <stdio.h>

 

 int add(int a, int b);

 

main()
{     
      int a,b;
     printf("Enter the two numbers: \n");

 

            scanf("%d",&a);
            scanf("%d",&b);
            printf("Sum of  given 2 numbers is: %d",add(a,b)); 

}

 

 int add(int a, int b)
 {
            if (!a)
                return b;
            else
   {
    int x = (a & b)<<1;
    int y = a ^ b;
    int z = add(x,y);
    return z;
   }
 }

 

Thanks & Regards,
Vidyasagar

 

MANOHAR

>>>>>>>> 

 

function prog()

a=40;

b=13;

ta=a;

tb=b;

v=0;

c=0;

clc

for i=1:16

    s=bitxor(bitxor(bitand(a,1),bitand(b,1)),bitand(c,1));

    v=v+s*(2^(i-1));

    c=bitor(bitor(bitand(bitand(a,1),bitand(b,1)),bitand(bitand(a,1),bitand(c,1))),bitand(bitand(b,1),bitand(c,1)));

    a=bitshift(a,-1);

    b=bitshift(b,-1);

end

 

disp(v);

 

>>>>>>>>>>>>>>>>> 

ABHINAY

>>>>>>>>>>>>>>>> 

a=0;

x=(dec2bin(5))

y=(dec2bin(200))

t='';

if length(x)<8

  for i=1:(8-length(x))

  t(i)='0';

  end

else

    t='';

end

x=seqreverse([t x]);

t='';

if length(y)<8

    for i=1:(8-length(y))

    t(i)='0';

    end

else

    t='';

end

y=seqreverse([t y]);

for i=1:8

    add(i)=bitxor(bitxor(str2num(x(i)),str2num(y(i))),a);

    a=bitor(bitor(bitand(str2num(x(i)),str2num(y(i))),bitand(str2num(x(i)),a)),bitand(str2num(y(i)),a));

end

ad=seqreverse(add);

result=bin2dec(strrep(num2str(ad),' ',''))

 

>>>>>>>>>>> 

SWETHA gave me logic but it is not acceptable.

 

>>>>>>>>> 

BHIMSEN JOSHI

>>>>>>>>>> 

#include<stdio.h>

 

void main()

{

      int i;

      int a = 255,b = 256;

 

      int s=0,c=0,k, ai, bi ;

 

      for(i=0;i<9;i++)

      {

            ai = ((a>>i)&1);

            bi = ((b>>i)&1);

            k = ((((ai^bi)^c))<<i);

            s = s|k;

          c = (ai&c) | (bi&c) | (ai&bi); 

      }

 

      printf("%d",s);

 

}

 

 

 


From: bhimsen [mailto:bhimsen@cmcltd.com]
Sent: Thursday, July 17, 2008 11:30 AM
To: 'bhimsen'; 'bhimsen'; 'Manohar'; 'Srikanth P'; 'Abhniay CMC'; 'K_Suresh_CMC'; 'Kattamuri_CMC'; 'Kiran K_CMC'; 'Mani_CMC'; 'Meena_CMC'; 'Murali_CMC'; 'Pavan Kumar G'; 'Pavani Madhira CMC'; 'Rajani G'; 'rrk@cmcltd.com'; 'Sadalakshmi M CMC'; 'Sivakumar'; 'Sravan_B'; 'SravanKumar_GV'; 'sravanthi.batchu@cmcltd.com'; 'Sriraja kumari'; 'Swetha Tattari CMC'; 'Usha_CMC'; 'V.Satish_CMC'; 'Venkat Prasad D'; 'Venkata Krishna R'; 'Vidyasagar_CMC'; 'Vijay_CMC'; 'VLV Naresh Kumar'; 'Santosh Kumar'; 'Venkata Krishna R'; 'V.Satish_CMC'
Subject: RE: C question # 2 - send answers only to me
Importance: High

 

Is every one busy or cannot solve the simple problem?

  None of the team members answered it correctly & completely.

 

I was thinking it is a 15 mins problem.

 

I received only one answer from Kiran.

 


From: bhimsen [mailto:bhimsen@cmcltd.com]
Sent: Wednesday, July 16, 2008 1:06 PM
To: 'bhimsen'; 'Manohar'; 'Srikanth P'; 'Abhniay CMC'; 'K_Suresh_CMC'; 'Kattamuri_CMC'; 'Kiran K_CMC'; 'Mani_CMC'; 'Meena_CMC'; 'Murali_CMC'; 'Pavan Kumar G'; 'Pavani Madhira CMC'; 'Rajani G'; 'rrk@cmcltd.com'; 'Sadalakshmi M CMC'; 'Sivakumar'; 'Sravan_B'; 'SravanKumar_GV'; 'sravanthi.batchu@cmcltd.com'; 'Sriraja kumari'; 'Swetha Tattari CMC'; 'Usha_CMC'; 'V.Satish_CMC'; 'Venkat Prasad D'; 'Venkata Krishna R'; 'Vidyasagar_CMC'; 'Vijay_CMC'; 'VLV Naresh Kumar'; 'Santosh Kumar'; 'Venkata Krishna R'; 'V.Satish_CMC'
Subject: C question # 2 - send answers only to me

 

Write a program to add two numbers <= 255

 

Constraint you cannot use arithmetic operators (+,-,*, /)

Use the Boolean operators (&, | ) and shift operators >>, << to achieve the result.

 

Regards,

Bhimsen joshi




--
At a certain point there is no difference between Magic and Science.
-Bhimsen Joshi

1 comment:

Unknown said...

Include me too in the circulating list for such puzzles, At My gmail.