Question:
Write Addition of two numbers using Bitwise operators.
Answer:
#include<stdio.h>
int main() { int a,b,sum, carry; scanf("%d %d",&a,&b); sum = a ^ b; carry = a & b; while (carry != 0) { carry <<= 1; a = sum; b = carry; sum = a ^ b; carry = a & b; } printf("%d",sum); getchar(); }
Source: CoolInterview.com
Answered by: Vijay | Date: 1/23/2008
| Contact Vijay
a,b be the variables
b=a^b; a=a^b; b=a^b;
Source: CoolInterview.com
Answered by: RAJA | Date: 1/23/2008
| Contact RAJA
#include<stdio.h> #include<conio.h> int add(int,int); main() { int a,b,sum; printf("Enter values of a,b"); scanf("%d %d",&a,&b); sum=add(a,b); printf(" %d",sum); getch(); } int add(int a,int b) { int x,y; do { x=a^b; y=a&b; a=x; b=y<<1; }while(b); return(x); } Source: CoolInterview.com
Answered by: uppala mamatha | Date: 1/23/2008
| Contact uppala mamatha
int main() { int num1,num2; printf("Enter Two Number "); scanf("%d %d",&num1,&num2); printf("ADD = %d ",(num1^num2)|((num1&num2)<<1)); return 0; } Source: CoolInterview.com
Answered by: Siva Chaitanya | Date: 1/29/2008
| Contact Siva Chaitanya
#include<stdio.h> void main() { int x,y,z; printf("Enter two no :"); scanf("%d%d",&x,&y); while(y) { z=x^y; y=(x&y)<<1; x=z; } printf("%d",z); } Source: CoolInterview.com
Answered by: ravi pandit | Date: 2/1/2008
| Contact ravi pandit
#include<stdio.h> main() { int a,b,c; printf("Read values from keyboard (a&b): "); scanf("%d%d",&a,&b); c=(a==b || a!=b)?printf("%d",a+b):printf("%d",a+b); } Source: CoolInterview.com
Answered by: ameya | Date: 8/22/2008
| Contact ameya
char given = 0x56; // let it be any two number(2 nibble character) char leftpart = 0x00; char rightpart = 0x00; char multiplication = 0x00; leftpart = given >> 4;
rightpart = given << 4; rightpart = rightpart >> 4; printf("left hand number is %x ", leftpart); printf("right hand number is %x ", rightpart);
multiplication = (leftpart&1?rightpart:0) + (leftpart&2?rightpart<<1:0) + (leftpart&4?rightpart<<2:0) + (leftpart&8?rightpart<<3:0); printf("multiplication is %d", multiplication ); Source: CoolInterview.com
Answered by: Sachin Magdum | Date: 1/29/2009
| Contact Sachin Magdum
#include<stdio.h> main() { int a,b,c; prinf("enter two num%d %d",a,b); c=b; b=a^b; a=a^b; c=(a^b)^c; printf("%d",c); getch(); } Source: CoolInterview.com
Answered by: Kiran | Date: 2/5/2010
| Contact Kiran
Hey kiran your code is wrong your logic is not working
#include<stdio.h> main() { int a,b,c; prinf("enter two num%d %d",a,b); c=b; b=a^b; a=a^b; c=(a^b)^c; printf("%d",c); getch(); }
Posted by: Kiran Source: CoolInterview.com
Answered by: dd | Date: 4/15/2010
| Contact dd
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|