Skip to content

Instantly share code, notes, and snippets.

@thommyj
Created March 15, 2013 08:02
Show Gist options
  • Save thommyj/5168214 to your computer and use it in GitHub Desktop.
Save thommyj/5168214 to your computer and use it in GitHub Desktop.
#include <stdio.h>
void displayOptions(char *name){
printf("\r\nUsage: %s word1 word2\r\n",name);
}
void printfb(unsigned int w){
int i;
for(i=31;i>-1;i--){
if(w >= (1<<i)){
printf("1 ");
w -= 1<<i;
}else{
printf("0 ");
}
}
}
void printd(unsigned int w1,unsigned int w2){
int i;
unsigned int diff = w1^w2;
//TODO: use printfb
for(i=31;i>-1;i--){
if(diff >= (1<<i)){
printf("| ");
diff -= 1<<i;
}else{
printf(" ");
}
}
}
void printbitnumber(){
int i;
for(i=0;i<32;i++){
printf("%2d ",i);
}
}
int main(int argc,char **argv)
{
unsigned int w1,w2;
if(argc<3){
displayOptions(argv[0]);
return 0;
}
w1 = strtoul(argv[1],NULL,0);
w2 = strtoul(argv[2],NULL,0);
printf("\r\n ");
printbitnumber();
printf("\r\nword1 0x%08X ",w1);
printfb(w1);
printf("\r\n");
printf(" ");
printd(w1,w2);
printf("\r\nword2 0x%08X ",w2);
printfb(w2);
printf("\r\n");
printf(" ");
printbitnumber();
printf("\r\n\r\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment