0

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;
}
  • Possible duplicate of [how to print binary number via printf](http://stackoverflow.com/questions/6373093/how-to-print-binary-number-via-printf) – jdigital Feb 23 '17 at 04:29
  • You have shown us the actual output, but what is the *expected* output? What is `showbits` supposed to do? Have you tried using a debugger to step through the code (when you do that it helps to make the code lense terse, for example by breaking up the `showbits` single line into multiple lines and using temporary variables)? – Some programmer dude Feb 23 '17 at 04:30
  • you mean you want to display in binary for a given integer? This is duplicate of [Print an int in binary representation using C](http://stackoverflow.com/questions/1024389/print-an-int-in-binary-representation-using-c) – Milind Deore Feb 23 '17 at 04:30
  • It looks like you're creating an integer that "looks" like a binary value but isn't. This won't work well. See some of the above links about how to print binary. For printing purposes, you can either print one bit at a time, or else build a string of ones and zeros and print that. – jdigital Feb 23 '17 at 04:31
  • 1
    The trouble is that your calculation of the binary value is overflowing. You need 11 bits to represent 1234; therefore, you'd need an 11-digit decimal number to store the representation of that in 'decimal-encoded binary'. Typically, a 32-bit `int` stores up to 10 decimal digits — so the extra one overflows. It's worse for the 19-bit shifted value. If you enter a smaller number (for example, 234, and shift of 8), the binary of the plain number is presented correctly (but the shifted value is wrong). If you enter just 0, 1, 2, or 3, it works correctly; any larger number gives you gibberish. – Jonathan Leffler Feb 23 '17 at 04:38
  • Possible duplicate of [Print an int in binary representation using C](http://stackoverflow.com/questions/1024389/print-an-int-in-binary-representation-using-c) – jww Feb 25 '17 at 05:51

1 Answers1

0

Because binary of 1234 is 10011010010 which can not be stored in an integer variable on your machine. If you change return type of showbits() to long, it might work. But this is not a permanent solution to get binary value.

MayurK
  • 1,925
  • 14
  • 27