I'm trying to create a function that returns the binary equivalent of a given integer. But when given the test input of 1234, it returns the incorrect value of both the given input's binary and the left-shifted version's binary:
Enter a base-10 integer: 1234
Enter number of places to shift bit: 8
1234 1421075418
315904 1826920960
The given value and left-shifted value are correct, but the binary equivalents aren't.
int showbits(int value) {
return (value == 0 || value == 1 ? value : ((value % 2) + 10 * showbits(value / 2)));
}
void left_bit_shift() {
int value = get_single_base_10();
int number_shift = get_number_shift();
int bit_value = showbits(value);
int left_shifted_value = value << number_shift;
int left_shifted_bit = showbits(left_shifted_value);
printf("%d %d\n", value, bit_value);
printf("%d %d", left_shifted_value, left_shifted_bit);
}
int get_single_base_10() {
int value;
printf("Enter a base-10 integer: ");
scanf("%d", &value);
return value;
}
int get_number_shift() {
int shift;
printf("Enter number of places to shift bit: ");
scanf("%d", &shift);
return shift;
}