somjit{} 60 Junior Poster in Training Featured Poster

i was searching for a code to perform binary XOR addition.
Exams coming up, and needed to a quick way to create and verify standard array tables. However, i didnt find anything substantial on the internet, so made my own version of it.

can anyone take a look and tell me if there's some other(better) way to do this?
thanks in advance for your time :)
somjit.

here's the code:

#include<stdio.h>
#include<string.h>

int main(){
    int *stop,decNum1=0,decNum2=0,result=0,ip_base=2,flag=0,i=0,binLength=0,temp=0,opResult[15];
    char num1[15],num2[15];

    puts("\n");

    do{
        printf("enter two binary numbers of same length(<15):\n");

        printf(" enter 1st binary number: ");
        fgets(num1,15,stdin);
        binLength=strlen(num1);
        decNum1=strtol(num1,&stop,ip_base);// decNum1 is the dec equivalent of binary input num1(a string)

        printf(" enter 2nd binary number: ");
        fgets(num2,15,stdin);
        decNum2=strtol(num2,&stop,ip_base);

        result=decNum1^decNum2; // dec equivalent of XOR addition
        printf("\n xor output:\n\n ");

        for(i;i<binLength;i++){ // converts the dec back to binary
            temp = result%2;
            result=result/2;
            opResult[i]=temp;
        }

        for(i=binLength-1;i>=0;i--){ // prints the result in the correct order
            printf("%d",opResult[i]);
        }


        printf("\n\n repeat? 1.yes 0.no\n ");
        scanf("%d",&flag);
        while((i=getchar())!='\n'){}
        i=0;

    }while(flag==1);

    return 0;
}

i hope someone finds the code useful. :) and for anyone who might be in a tiny confusion, Info & Coding theory more generally refers binary XOR as "modulo-2 addition". :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.