Advertisement
defineSN

XOR binary addition.

Nov 30th, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.05 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. int main(){
  5.     int *stop,decNum1=0,decNum2=0,result=0,ip_base=2,flag=0,i=0,binLength=0,temp=0,opResult[15];
  6.     char num1[15],num2[15];
  7.    
  8.     puts("\n");
  9.    
  10.     do{
  11.         printf("enter two binary numbers of same length(<15):\n");
  12.        
  13.         printf(" enter 1st binary number: ");
  14.         fgets(num1,15,stdin);
  15.         binLength=strlen(num1);
  16.         decNum1=strtol(num1,&stop,ip_base);// decNum1 is the dec equivalent of binary input num1(a string)
  17.        
  18.         printf(" enter 2nd binary number: ");
  19.         fgets(num2,15,stdin);
  20.         decNum2=strtol(num2,&stop,ip_base);
  21.        
  22.         result=decNum1^decNum2; // dec equivalent of XOR addition
  23.         printf("\n xor output:\n\n ");
  24.        
  25.         for(i;i<binLength;i++){ // converts the dec back to binary
  26.             temp = result%2;
  27.             result=result/2;
  28.             opResult[i]=temp;
  29.         }
  30.        
  31.         for(i=binLength-1;i>=0;i--){ // prints the result in the correct order
  32.             printf("%d",opResult[i]);
  33.         }
  34.        
  35.            
  36.         printf("\n\n repeat? 1.yes 0.no\n ");
  37.         scanf("%d",&flag);
  38.         while((i=getchar())!='\n'){}
  39.         i=0;
  40.        
  41.     }while(flag==1);
  42.    
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement