Thread: Denary to binary converter

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    1

    Denary to binary converter

    I have been learning to program in C and decided that I would like to try making a denary to binary converter as this also included the use of inputs from the keyboard.

    After creating a code that was manual (had to enter the results for every denary number into the code, e.g. for 1 write 1, for 2 write 10, for 3 write 11, etc.) I decided to look at codes that could take any denary number and turn it into the binary equivalent.

    I found the following code (well, it was more complicated but I have stripped it down to what I wanted it to do and learnt lots in the process doing so) which I know works, however I do not really understand quite what is going on.

    I understand what the main() function is doing, however I do not understand what the denary_binary function is doing. If anyone could explain what this means I would be extremely grateful. If you are also able to clear up which parts are user-defined (e.g. is 'rem' a particular function or could it have been called anything?), that would be awesome too.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
        int n;
        int denary_binary(int n);
    
        printf("\nEnter a denary number.\n");
        scanf("%d", &n);
        printf("\n%d = %d in binary.\n\n", n, denary_binary(n));
       
        return 0;
    }
    
    int denary_binary(int n)
    {
        int rem, i=1, binary=0;
        while (n!=0)
        {
            rem=n%2;
            n/=2;
            binary+=rem*i;
            i*=10;
        }
        return binary;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by epsilon_
    I understand what the main() function is doing, however I do not understand what the denary_binary function is doing. If anyone could explain what this means I would be extremely grateful.
    I think that there is some confusion here because rather than storing the binary representation in a string, the denary_binary function stores the binary representation in an int, where the decimal (denary) representation of that int is used as the binary representation of the original value. For example, given an input of 5, a typical binary representation as a numeric string is "101", but the value returned from the function is 101 (i.e., one hundred and one).

    So, the idea is to extract the bits from the number, from the least significant bit to the most significant bit. These bits are then added to the result in their correct place with the help of the multiplier named i. Observe that i itself is multiplied by 10 on each iteration: it grows bigger as the bits get more significant, and the multiplication by 10 reflects the curious fact that the decimal representation of the result is used as the binary representation of the original value.

    Quote Originally Posted by epsilon_
    If you are also able to clear up which parts are user-defined (e.g. is 'rem' a particular function or could it have been called anything?), that would be awesome too.
    rem is a variable name, so indeed any other valid and unreserved name would have worked just as well, as long as it were sufficiently descriptive.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hex to Binary Converter
    By Fields in forum C Programming
    Replies: 12
    Last Post: 09-25-2012, 07:42 PM
  2. denary to binary problems
    By redruby147 in forum C Programming
    Replies: 6
    Last Post: 02-09-2009, 05:21 PM
  3. Decimal to Binary Converter
    By peckitt99 in forum C Programming
    Replies: 16
    Last Post: 10-12-2006, 05:25 AM
  4. binary converter
    By cerin in forum C++ Programming
    Replies: 5
    Last Post: 04-18-2005, 04:52 PM
  5. Binary converter
    By CheesyMoo in forum C++ Programming
    Replies: 2
    Last Post: 02-18-2003, 10:27 PM

Tags for this Thread