0

During left shift or right shift of a "decimal number", I want the result in binary format and not in decimal. I know how to convert decimal to binary. Is there any other way to capture intermediate binary values?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Abhishek
  • 1,585
  • 2
  • 12
  • 15
  • Not sure exactly what you're asking for, could you provide an example of what you want? – ryanpattison Feb 18 '15 at 05:53
  • This might help you http://stackoverflow.com/questions/2548282/decimal-to-binary-and-vice-versa – Thiyagu Feb 18 '15 at 05:53
  • 3
    What do you mean by **Intermediate Binary Values** ??? – Arun A S Feb 18 '15 at 05:55
  • Are you talking about binary left & right shift or decimal left & right shift? – Mike Nakis Feb 18 '15 at 05:57
  • @MikeNakis i am talking about Decimal Left/Right Shift – Abhishek Feb 18 '15 at 06:04
  • @ArunA.S by Intermediate i mean when we do left/right shift of Decimal Number, system first do it in Binary then provide us the Decimal Result, I want to capture that BINARY result – Abhishek Feb 18 '15 at 06:06
  • @rpattiso for example when we do left shift of 60 by 1 bit we get 120 as result, but system first do it in binary way and then provide us the Decimal value... I wan to capture that Binary value – Abhishek Feb 18 '15 at 06:09
  • It's not like you have to "intercept" anything, the number is always in binary internally (although that's irrelevant for anything but a performance and limits point of view). It's *converted to string* in decimal base by `printf`, you just have to print it in binary (using the usual algorithms). – Matteo Italia Feb 18 '15 at 06:24
  • @MatteoItalia yes I want to Intercept the binary format. Can i do that ? – Abhishek Feb 18 '15 at 06:26
  • 2
    Don't forget, most computers only work in binary. Decimal output requires you to convert the binary representation to decimal. That said, there isn't a standard way of printing binary. And by capturing 'intermediate results', do you mean that if you do `uint16_t x = 0x9876; x >>= 5;` then you want to see the result of `x >> 1` and `x >> 2` and `x >> 3` and `x >> 4` as well as the final `x >>= 5;`? If so, you're going to have to generate and print those intermediate values in a loop. – Jonathan Leffler Feb 18 '15 at 06:41
  • 2
    You are not shifting a *decimal number*, you are shifting *a number*. A number is a number, the fact that internally it's represented in binary (probably. Who knows how modern CPU registers are made? Maybe they work in base 4) impacts only the speed of certain operations. The numeric base is just a thing of external representation. Do you want base 10? Do the division/modulo by 10 loop to calculate each digit, which is what `printf` does internally. Do you want base 2? It's the same, but with 2 (which means that it can be optimized with shifts and masks). – Matteo Italia Feb 18 '15 at 06:50
  • @MatteoItalia Well, the shift is done to the binary representation, not to the number, as it wouldn't make sense otherwise. But still the number doesn't need to be intercepted, as it is still a number. Maybe he wants something like a `%b` format specifier for printing binary – alx - recommends codidact Apr 15 '19 at 21:59
  • This may help: https://stackoverflow.com/a/112947 – alx - recommends codidact Apr 15 '19 at 22:03

1 Answers1

1

Here's how to print in hexadecimal, which you can pretty easily translate to binary in your head.

printf("0x%x\n", my_int);

Here's code to print a number in a binary format:

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

int main(int argc, char **argv)
{
  char buf[65] = { 0 }, *endptr;
  long my_int;
  unsigned long tmp_int;
  int i, j;

  if (argc != 2 || (errno = 0, (my_int = strtol(argv[1], &endptr, 0)), errno) || *endptr)
    exit((fprintf(stderr, "Usage: %s <number>\n", argv[0]), 1));

  /* break my_int down into a binary character represenation */

  tmp_int = my_int;
  i = 0;

  do
  {
    buf[i++] = ((tmp_int & 0x1) ? '1' : '0');
    tmp_int >>= 1;
  }
  while (0 != tmp_int);

  /* reverse buf for printing */

  for (j = 0, --i; j < i; ++j, --i)
  {
    char c = buf[j];

    buf[j] = buf[i];
    buf[i] = c;
  }

  printf("%s\n", buf);

  return 0;
}
jschultz410
  • 2,849
  • 14
  • 22
  • 1
    Note: this is not "converting decimal to binary", it's just printing the number in binary, since the number was never decimal to start with. – user253751 Feb 18 '15 at 07:11
  • @jschultz410 Thanks for your answer. The thing is i know how to convert decimal to binary, What i want to know is how can i capture/intercept the binary value while i am doing the left/right shift conversion of a Decimal Number (as system actually do the conversion in binary format then provide Decimal value) – Abhishek Feb 18 '15 at 07:19
  • 1
    By decimal shift, do you mean multiplying or dividing a number by powers of ten? I don't know of any way to capture or intercept an intermediate value as the CPU operates on it, or even what that would mean. You can in your code draw a computation out into several discrete steps and print the result of each step in binary if you like. – jschultz410 Feb 18 '15 at 07:21