Skip to content

Instantly share code, notes, and snippets.

@peterbrendel
Created April 8, 2017 06:35
Show Gist options
  • Save peterbrendel/6950ea3fbf9cb52bc95152cc9178d4fa to your computer and use it in GitHub Desktop.
Save peterbrendel/6950ea3fbf9cb52bc95152cc9178d4fa to your computer and use it in GitHub Desktop.
#include <stdio.h>
void decToBin(unsigned int x, char * vect);
unsigned int binToDec(unsigned int r, char * vect);
unsigned int powerof(unsigned int n, int xp);
int main(void){
unsigned int x, y, i, r;
char binx[33] = {'\0'}, biny[33] = {'\0'}, binr[33] = {'\0'};
while(scanf("%d", &x) != EOF){
if(scanf("%d", &y) == EOF)
return 0;
decToBin(x, binx);
decToBin(y, biny);
for(i=0; i<32; i++){
if(binx[i] == biny[i] && binx[i] == '1')
binr[i] = '0';
else if(binx[i] != biny[i])
binr[i] = '1';
else
binr[i] = '0';
}
r = binToDec(r, binr);
printf("%u = %s\n%u = %s\n%u = %s\n", x, binx, y, biny, r, binr);
//printf("%u\n", r);
}
return 0;
}
void decToBin(unsigned int x, char *vect){
int i;
for(i=31; i>=0; i--){
vect[i] = 48 + (x % 2);
x/=2;
}
}
unsigned int binToDec(unsigned int r, char *vect){
int i;
r=0;
for(i=31; i>=0; i--)
if(vect[i] == '1')
r += powerof(2, (31-i));
return r;
}
unsigned int powerof(unsigned int n, int xp){
if(xp==0)
return 1;
else
return n*powerof(n, xp-1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment