Thread: (Help) A function that transforms a decimal number into binary

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    3

    (Help) A function that transforms a decimal number into binary

    I have an assignment to write a function that prints out a binary version of an entered number. This is the code I have so far:
    Code:
    #include <stdio.h>
    
    
    void printBinary(short n)
    {
        int r;
        short result, i;
        result=0;
        for (i=1; n!=0; i=i*10);
        {
            r=n%2;
            result=result+(r*i);
            n=n/2;
        }
        printf("Binary: %d ", result);
    }
    
    
    int main(int argc, char **argv)
    {
    short n;
    printf("Enter number: ");
    scanf("%hd", &n);
    printBinary(n);
    }
    For some reason the program works only when I enter 0. In all other cases it does nothing. Doesn't even give an error message, neither does the text "Binary: " appear. Any help is appreciated.

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Take a close look at the end of line 9.

  3. #3
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Good eye, mate! The dreaded "semicolon" problem, once again.

  4. #4
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Code:
    printf("Binary: %d ", result);
    That's not gonna print binary. There's no way in C to print an integer in binary format that I know of... and %d format prints decimal notation, in any case.
    Of course it works for "0"... because "0" in any number system is equivalent to "0" in any other. That is, 0(y) == 0(x) ... where x and y are arbitary bases.

    This is not a difficult problem. Given an integer, consider its binary representation in the computer. In order to write this program and understand how it works, you must already be able to manually convert an integer given in decimal notation to the equivalent value given in binary notation. Perhaps this is the real problem?

    You're on the right track by dividing by 2, until the decimal value becomes 0.

    HINT: The results of the division form the binary digits of the result, however they will be generated in reverse!

    OTOH, another, more efficient method would be to scan the actual bits of the integer value, thus avoiding the division operation. In other words, there is no call for a division operation here. It can be done with bitwise operations provided by the CPU... which are MUCH faster than general division.

    HINT 2: Think about it way... once the integer variable goes into the computer, its ALREADY CONVERTED to binary. Then one need only to scan the bits from left to right, producing a '0' or '1' value...
    Last edited by MacNilly; 02-13-2017 at 04:22 PM.

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You're trying to print the bits of a short, which is usually 16 bits. But you're trying to fit the "decimal mockup" (if I may) into a short, which has a max value of 32767. So only a max of 5 "deci-bits" will fit in there. Even an int is often only 32 bits with a max value of 2147483647. That's only room for 10 deci-bits.

    If you have your heart set on creating a decimal mockup of the bits then you'd have to use a 64-bit quantity (a long long, or int64_t from stdint.h), max value 9223372036854775807 (19 digits).

    You'd still have problems with a negative number if you have to handle negative values and print the (common) two's complement notation. In that case, you could receive a signed number and convert it to an unsigned value through reinterpretation of the underlying representation, then print the bits of the unsigned version.
    Code:
    unsigned short u = *(unsigned short*)&n;
    If you don't need the decimal mockup, then the usual procedure is to either store the 1's and 0's in a string (in reverse), or use a recursive function (printing 1 or 0 after each recursive call), or simply print out the bits as determined by bitwise-anding against a bit mask from the high bit downwards. The last approach is probably the best.

    BTW, don't define argc and argv if you're not using them. You should also explicitly return 0 at the end of main. And a newline would be nice after printing the output.
    Last edited by algorism; 02-13-2017 at 05:51 PM.

  6. #6
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Quote Originally Posted by algorism View Post
    Apparently he's trying to create a decimal number that looks like the binary representation of the given short. An unusual approach.
    Oh I see, I think maybe it makes sense.. hmm, no, not really. Maybe a problem when 2 == 10?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. binary number to decimal
    By Amos Bordowitz in forum C Programming
    Replies: 2
    Last Post: 03-30-2014, 02:27 PM
  2. Convert a binary number into a decimal number
    By HTHVampire in forum C++ Programming
    Replies: 7
    Last Post: 07-06-2013, 09:53 AM
  3. Convert 8-digit Binary Number to decimal number
    By yongsheng94 in forum C++ Programming
    Replies: 2
    Last Post: 07-06-2013, 09:47 AM
  4. Typing decimal number and cover it binary number!
    By Kazumi in forum C Programming
    Replies: 32
    Last Post: 04-16-2011, 07:21 PM
  5. Binary Number to Decimal
    By theCanuck in forum C++ Programming
    Replies: 12
    Last Post: 02-09-2011, 11:25 PM

Tags for this Thread