by using bitwise operators it is possible, for example a=8,ie in binary(00001000) & b=4 in binary (00000100)if we add a,b then we get 12 in binary(00001100)..if u add the binary a,b & convert the answer in to decimal we get 12.. in the c code is: void main() { int a=8,b=4,c; clrscr(); c=a^b; printf("%d",c); getch(); } so,by using ^ operator we can do that in c.
main() { int i,j,k; printf("Enter the value of i and j"); scanf("%d%d",&i,&j); if(i<j) { for(k=0;k<i;k++) j++; } else { for(k=0;k<j;k++) i++; } printf("The required sum is : ",i) }
#include<stdio.h> unsigned int add(unsigned int a, unsigned int b) { unsigned int c=0; unsigned int r=0; unsinged int t=~0; for (t=~0;t;t>>=1) { r<<=1; r|=(a|b)&c|a&b)&1; a>>=11; b>>=1; } for(t=~0,c=~t;t;t>>=1) { c<<=1; c|=r&1; r>>1; } int main(int argc,char*argv[]) { printf("%d+%d=%d ",atoi(argv[1]),atoi(argv[2]),add(atoi(argv[1]),atoi(argv[2]))); return(0); }
int add(int a, int b) { int sum, carry; sum = a ^ b; carry = a & b; while (carry != 0) { carry <<= 1; a = sum; b = carry; sum = a ^ b; carry = a & b; } return sum; }
#include<stdio.h> void main() { int num1 = 10; //First Number int num2 = 5; //Second Number int i; // loop counter int s=0; for (i=0;i<5;i++) { i++; s++; } printf("Sum = %d",s); }
#include <stdio.h> int add(int a, int b){ char carry=0; int result=0,bitshift=0; while ((a!=0)||(b!=0)){ result=(((a&0x1)^(b&0x1)^carry)<<bitshift)|result; if (((a&0x1)&(b&0x1))||((a&0x1)&carry)||((b&0x1)&carry)) carry=1;else carry=0; a=a>>1;b=b>>1;bitshift++; } result=(carry<<bitshift)|result; return result; } int main(int argc, char **argv) {int a=255,b=12; printf("Total of %d and %d is: %d",a,b,add(a,b));getchar(); return 0; }
#include<stdio.h> main() { int i,j,k; printf("Enter the value of i and j"); scanf("%d%d",&i,&j); if(i>j) { for(k=0;k<i;k++) {j++; } printf("The required sum is : %d",j); } else { for(k=0;k<j;k++) { i++; } printf("The required sum is : %d",i); } }
#include<stdio.h> main() { int a,b,i; clrscr(); printf("enter the value of a and b"); scanf("%d %d",&a,&b); int d=a; for(i=1;i<=b;i++) { a++; } if(b==0) printf("%d",d); else printf("%d",a); }
void main() { float a= 0.7; if (a < 0.7) printf("c"); else printf("c++"); } Output of the above program is c. Why? Whereas the same program with 0.8 instead of 0.7 gives c++ as the output? Why explain?