Last active
December 11, 2015 12:58
-
-
Save hcccc/2b055fd7ba2b58d88c9e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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