C - Write program to apply XOR operation twice

Requirements

Write program to apply XOR operation twice

Hint

If you use the same XOR value on a variable twice, you get back the variable's original value.

Demo

#include <stdio.h>

char *to_binary(int n);

int main()/*from w  ww  .j  ava 2s  . c o  m*/
{
    int a,x,r;

    a = 73;
    x = 170;

    printf("  %s %3d\n",to_binary(a),a);
    printf("^ %s %3d\n",to_binary(x),x);
    r = a ^ x;
    printf("= %s %3d\n",to_binary(r),r);
    printf("^ %s %3d\n",to_binary(x),x);
    a = r ^ x;
    printf("= %s %3d\n",to_binary(a),a);
    return(0);
}

char *to_binary(int n)
{
    static char bin[9];
    int x;

    for(x=0;x<8;x++)
    {
        bin[x] = n & 0x80 ? '1' : '0';
        n <<= 1;
    }
    bin[x] = '\0';
    return(bin);
}

Result

Related Exercise