Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- char* to_bin_str(int num)
- {
- char temp[2];
- char bin_str[33] = {0};
- int shifts = 31;
- int bit;
- temp[1] = '\0';
- while(shifts >= 0)
- {
- bit = num & (1 << shifts);
- shifts--;
- temp[0] = (bit != 0) + '0';
- strcat(bin_str, temp);
- }
- return strdup(bin_str);
- }
- int main()
- {
- int num = 0;
- char* bin_str = NULL;
- scanf("%d", &num);
- bin_str = to_bin_str(num);
- printf("%s\n", bin_str);
- free(bin_str);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement