-4

Basically I want to learn the algorithm on how to convert decimal to binary, I found this:

int convert(int dec)
{
    if (dec == 0)
    {
        return 0;
    }
    else
    {
        return (dec % 2 + 10 * convert(dec / 2));
    }
}

It works just fine, but I am not able to understand dec % 2 + 10 * convert(dec / 2). Can you please convert this in an understandable way for people with basic math? e.g. what method is performed first and how does the binary dec = 50 turns to 110010?

FYI: I can do it, this way: 50=(2^5=32)+(2^4=16)+(2^1)=50

Thanks in advance.

aaaa
  • 25
  • 1
  • 6
  • 1
    This is not converting to binary, it is converting to decimal which *looks* like binary. And yes, it is using recursion. – Eugene Sh. Aug 16 '18 at 15:49
  • 1
    (ec) Beware of this code. This code is teaching you a very confusing, ultimately wrong lesson. This code appears to convert, for example, the decimal number 5 into the decimal number 101. That sort of *looks* like decimal-to-binary conversion, but it's not. – Steve Summit Aug 16 '18 at 15:50
  • @SteveSummit thanks for informing me on about that, can you please redirect me to a proper lesson or what algorithm does computers use to convert decimal to binary? – aaaa Aug 16 '18 at 15:51
  • It would probably make the most sense to walk through the code for some example input (either using a debugger or by hand). – Bernhard Barker Aug 16 '18 at 15:52
  • @aaaa I was going to say, "Why not try a Google search", but I tried it myself, and the very first hit was (I guess not surprisingly) the very same broken code you posted. – Steve Summit Aug 16 '18 at 15:53
  • @aaaa There are probably thousands of better examples scattered across the net, but I don't think I have a handle just now on one I like. – Steve Summit Aug 16 '18 at 15:56
  • `1101011 % 2 = 1`. `1101011 / 2 = 110101`. `10*1234 + 5 = 12345`. The code is a bit confusing, but it shouldn't be too hard to see what's going on here by just looking at the results you get when performing those operations. – Bernhard Barker Aug 16 '18 at 16:01
  • 1
    Possibly not a duplicate of [Is there a printf converter to print in binary format?](https://stackoverflow.com/questions/111928/is-there-a-printf-converter-to-print-in-binary-format) but there is much there of relevance. – High Performance Mark Aug 16 '18 at 16:09
  • An integer is already stored in a binary format. What (I think) you're really asking is how to print it as a string of ones and zeroes – Tim Randall Aug 16 '18 at 16:27
  • @TimRandall, `Basically I want to learn the algorithm on how to convert decimal to binary` what is so confusing about that? – aaaa Aug 16 '18 at 16:33
  • Then leave C alone and look for a pen-and-paper method. It is very same one, but decoupled from programming. – Eugene Sh. Aug 16 '18 at 16:34
  • @EugeneSh. did I say I need a c program to convert dec to bin? I said an algorithm to convert dec to bin? FYI: I have remove `c` tag. – aaaa Aug 16 '18 at 16:39
  • I have provided the algorithm as requested. Apologies if you got the impression that I was confused. I was, perhaps, under a similar misconception. – Tim Randall Aug 16 '18 at 17:14

3 Answers3

2

I won't implement it for you, but I am happy to describe the algorithm and give an example.

Converting from base 10 to base b ultimately follows the same series of steps which includes repeatedly dividing by b then saving the remainder.

An example of what this looks like for 50 (base10) to base2 would be:

          Quotient  Remainder  
----------------------------
50 / 2 =  25        0
25 / 2 =  12        1
12 / 2 =   6        0
 6 / 2 =   3        0
 3 / 2 =   1        1 
 1 / 2 =   0        1

Examining the remainders in reverse (bottom to top) gives your the correct representation in base b (2 in this case): 110010

For information on why this works, take a look at this question: https://math.stackexchange.com/questions/86207/converting-decimalbase-10-numbers-to-binary-by-repeatedly-dividing-by-2

Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
  • Thanks that almost answered my question, Just need to know what algorithm does machines use to convert dec to bin or vice versa? – aaaa Aug 16 '18 at 16:15
  • 1
    Machines don't need to convert anything, they store binary and work with binary. – Eugene Sh. Aug 16 '18 at 16:28
  • @EugeneSh. okay so let's say someone on his keyboard pressed `A` which is registered as decimal # `41` then how does computer know `41` is `101001` and need to perform Functions for character `A`? – aaaa Aug 16 '18 at 16:36
  • 2
    *which is registered as decimal # 41* - it is not. It is registered as a number, which if represented as decimal, will look like `41`, or if is represented as binary will look as `101001`, or if represented as hexadecimal, will look as `0x29`... – Eugene Sh. Aug 16 '18 at 16:38
  • 2
    @aaaa That seems like a harsh, especially considering that everything EugeneSh said is correct. What we see as characters typed on a keyboard your computer sees as numbers. – Hunter McMillen Aug 16 '18 at 16:52
  • 2
    @aaaa also, typically when you ask for help on the internet it pays off to not be rude to the people attempting to help you – Hunter McMillen Aug 16 '18 at 16:52
  • The computer naturally works with binary numbers. That's why we refer to processors as 32-bit, 64-bit, etc. We are literally referring to the number of binary digits that the processor can handle in its registers. When you press 'A' on your keyboard, the machine might receive a scan code 00011110 which is written in hex as 0x1e or in decimal as 30. But the machine sees those eight bits - those eight binary digits - not any other representation. – Tim Randall Aug 16 '18 at 17:22
0

Let's look at dec % 2 + 10 * convert(dec / 2). The first part dec % 2 is a modulo operation, and this is what decides if a digit should be 1 or 0. The rest, 10 * convert(dec / 2) finds the next (and next and next recursively) digit and puts it on the left of the current digit.

You could quite easily see what is going on by slightly modifying your code. Change the else to:

else
{
    int ret = (dec % 2 + 10 * convert(dec / 2));
    printf("%d %d\n", dec, ret);
    return ret;
}

and then convert(50) will print this:

$ ./a.out 
1 1
3 11
6 110
12 1100
25 11001
50 110010

But as pointed out in the comments, this is not a real base conversion. You have converted the number 50 to a completely different number that looks like the binary representation.

klutt
  • 30,332
  • 17
  • 55
  • 95
0

An algorithm that will, given an integer N, produce a string of characters S representing N in binary notation.

do
{
    if N is odd
    {
        add '1' to the beginning of S
    }
    else
    {
        add '0' to the beginning of S
    }
    divide N by 2
}
while N is non-zero

Using the requested example:

initially N=50 and S is empty
50 is even: S="0"
divide N by 2: N=25
25 is odd: S="10"
divide N by 2: N=12
12 is even: S="010"
divide N by 2: N=6
6 is even: S="0010"
divide N by 2: N=3
3 is odd: S="10010"
divide N by 2: N=1
1 is odd: S="110010"
divide N by 2: N=0
stop looping
Tim Randall
  • 4,040
  • 1
  • 17
  • 39