2022-09-19 17:13:21 +09:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
void fallback_print_usage() {
|
|
|
|
printf("Usage: ./convert [int|long|float|double] number\n");
|
|
|
|
printf("Example: ./convert float 3.14\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_int(int x) {
|
|
|
|
// one character per one bit
|
|
|
|
char output[32 + 1] = {
|
|
|
|
0,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* YOUR CODE START HERE */
|
|
|
|
int k;
|
2022-10-04 21:07:02 +09:00
|
|
|
unsigned int value = *((unsigned int *)&x);
|
2022-09-19 17:13:21 +09:00
|
|
|
size_t bits = 8 * sizeof(int);
|
|
|
|
for (k = 0; k < bits; ++k) {
|
2022-10-04 21:07:02 +09:00
|
|
|
output[bits - 1 - k] = (value & (1ul << k)) ? '1' : '0';
|
2022-09-19 17:13:21 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/* YOUR CODE END HERE */
|
|
|
|
|
|
|
|
printf("%s\n", output);
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_long(long x) {
|
|
|
|
// one character per one bit
|
|
|
|
char output[64 + 1] = {
|
|
|
|
0,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* YOUR CODE START HERE */
|
|
|
|
int k;
|
2022-10-04 21:07:02 +09:00
|
|
|
unsigned long value = *((unsigned long *)&x);
|
2022-09-19 17:13:21 +09:00
|
|
|
size_t bits = 8 * sizeof(long);
|
|
|
|
for (k = 0; k < bits; ++k) {
|
2022-10-04 21:07:02 +09:00
|
|
|
output[bits - 1 - k] = (value & (1ul << k)) ? '1' : '0';
|
2022-09-19 17:13:21 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/* YOUR CODE END HERE */
|
|
|
|
|
|
|
|
printf("%s\n", output);
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_float(float x) {
|
|
|
|
// one character per one bit
|
|
|
|
char output[32 + 1] = {
|
|
|
|
0,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* YOUR CODE START HERE */
|
|
|
|
int k;
|
2022-10-04 21:07:02 +09:00
|
|
|
unsigned int value = *((unsigned int *)&x);
|
2022-09-19 17:13:21 +09:00
|
|
|
size_t bits = 8 * sizeof(int);
|
|
|
|
for (k = 0; k < bits; ++k) {
|
2022-10-04 21:07:02 +09:00
|
|
|
output[bits - 1 - k] = (value & (1ul << k)) ? '1' : '0';
|
2022-09-19 17:13:21 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/* YOUR CODE END HERE */
|
|
|
|
|
|
|
|
printf("%s\n", output);
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_double(double x) {
|
|
|
|
// one character per one bit
|
|
|
|
char output[64 + 1] = {
|
|
|
|
0,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* YOUR CODE START HERE */
|
|
|
|
int k;
|
2022-10-04 21:07:02 +09:00
|
|
|
unsigned long value = *((unsigned long *)&x);
|
2022-09-19 17:13:21 +09:00
|
|
|
size_t bits = 8 * sizeof(long);
|
|
|
|
for (k = 0; k < bits; ++k) {
|
2022-10-04 21:07:02 +09:00
|
|
|
output[bits - 1 - k] = (value & (1ul << k)) ? '1' : '0';
|
2022-09-19 17:13:21 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/* YOUR CODE END HERE */
|
|
|
|
|
|
|
|
printf("%s\n", output);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
if (argc != 3)
|
|
|
|
fallback_print_usage();
|
|
|
|
if (strcmp(argv[1], "int") == 0) {
|
|
|
|
print_int(atoi(argv[2]));
|
|
|
|
} else if (strcmp(argv[1], "long") == 0) {
|
|
|
|
print_long(atol(argv[2]));
|
|
|
|
} else if (strcmp(argv[1], "float") == 0) {
|
|
|
|
print_float(atof(argv[2]));
|
|
|
|
} else if (strcmp(argv[1], "double") == 0) {
|
|
|
|
print_double(atof(argv[2]));
|
|
|
|
} else {
|
|
|
|
fallback_print_usage();
|
|
|
|
}
|
|
|
|
}
|