Skip to content

Instantly share code, notes, and snippets.

@hcccc
Last active December 11, 2015 12:58
Show Gist options
  • Save hcccc/2b055fd7ba2b58d88c9e to your computer and use it in GitHub Desktop.
Save hcccc/2b055fd7ba2b58d88c9e to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BITMASK(n) (1 << (n))
void divide_and_print(int x){
printf("Dec: %d\n", x);
printf("Hex: 0x%X\n", x);
printf("Bin: ");
int found_first_one = 0;
int i = 31;
for(i = 31; i >= 0; i--){
if(x&BITMASK(i)){
printf("1");
if(found_first_one == 0){
found_first_one = 1;
}
}
else if(found_first_one){
printf("0");
}
}
printf("\n");
}
int main(int argc, char *argv[]){
if(argc != 2){
printf("[Usage]: Input a valid Dec number\n");
return -1;
}
int x = atoi(argv[1]);
divide_and_print(x);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment