Like Tree1Likes

decimal to binary conversion

This is a discussion on decimal to binary conversion within the C Programming forums, part of the General Programming Boards category; Originally Posted by roelof Oke, I understand that, But how can I know if a number is a 32 bit ...

  1. #16
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by roelof View Post
    Oke,

    I understand that,
    But how can I know if a number is a 32 bit binary or a 64 bit binary.
    In my opinion we can know that after the conversion.

    Roelof
    You know that by knowing about your compiler... in my compiler int is 32 bits... I know this and I can trust it.
    If your compiler's library includes stdint.h you can be very explicit about variable sizes... for example int32_t guarantees a 32 bit integer.

    If you are concerned about the user's entry overflowing the integer's range... you need to limit your user's input to acceptable values (almost always a necessity in real world programming, anyway)... There are a great many ways of doing this but in a training exercise it's safe to make a note to work on this later (once your more familiar with the language)

  2. #17
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    So I can't know if I have to have to array of 32 of 64 bit.

    Roelof

  3. #18
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by roelof View Post
    Oke,

    So I can't know if I have to have to array of 32 of 64 bit.

    Roelof
    Again... you know this because you are telling the compiler what to do... there's no way to back-track and discover this later. The C compiler won't track this for you and it won't produce errors if you mess up. It is up to you as a programmer to keep track of the variables and their sizes.

    Here's a little example of limiting input to acceptable values, based on my previous example... (Note this is untested but it should be pretty close...)

    Code:
    #include <stdint.h>
    #include <stdio.h>
    
    int main (void)
      { int64_t number;  // 64 bit input space
         int32_t  filter = 0x40000000u;  // 32bits, msb = 1;
    
         while (number > -1)
          { printf("Enter a number from 0 to %d  (-1 to exit) :", INT32_MAX);
    
             // read in a 64 bit value
             scanf("%lld", number);
    
             // get rid of extraneous newline character
             getchar(); 
    
             // range check
             if ((number > -1) && (number <= INT32_MAX)
               { printf("\n\n");
                  break; } }
    
          // exit on any negative number
          if (number < 0)
            exit(0);
    
          printf("Your number as binary is :   ");
    
          // burst to binary
          while (filter)
             { if (number & filter)
                 printf("1");
               else
                 printf("0");
               filter = filter >> 1; }
    
          printf("\n\n");
    
       return 0; }
    Last edited by CommonTater; 05-14-2011 at 12:18 PM. Reason: aligning text

  4. #19
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    I hope my book (C book) will explain that later.
    It's not something chapter 1 has explained.
    They only talked about char and int.

    But thanks for the explanation.

    Roelof

  5. #20
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by roelof View Post
    Oke,

    So I can't know if I have to have to array of 32 of 64 bit.

    Roelof
    The C standard requires your compiler to tell you which it is in the manual/other documentation that came with the compiler. So assuming you know where you put the manual/other documentation, there will be a section about "implementation limits" or some such that says whether "int" is 32 or 64 bits.

  6. #21
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    And if I understand it right
    It can be that on my system int us 64 bit and on another system int = 32 bit.
    So as Commentator have said it better that I say if a int is 32 or 64 bit.
    Otherwise wierd things can happen.

    Roelof

  7. #22
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by roelof View Post
    Oke,

    I hope my book (C book) will explain that later.
    It's not something chapter 1 has explained.
    They only talked about char and int.

    But thanks for the explanation.

    Roelof
    No worries... In your place I'd make a note to check into it a bit later in the learning process... When I was learning C++ (a recent undertaking) I made lots of notes about stuff I have to review and investigate later. It's all part of wanting to learn....

  8. #23
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by roelof View Post
    Oke,

    And if I understand it right
    It can be that on my system int us 64 bit and on another system int = 32 bit.
    So as Commentator have said it better that I say if a int is 32 or 64 bit.
    Otherwise wierd things can happen.

    Roelof
    It doesn't always matter... what do you care when simply counting to 10? But when doing binary manipulations or dealing with really big numbers, yes, it is better.

    FWIW... there are some cases where an int = 16 bits, too.

    Although I doubt your textbook has covered it yet, you can always do this:
    Code:
    int x = sizeof(int):
    The value of x is in characters (bytes) so...

    2 = 16 bit integers
    4 = 32 bit integers
    8 = 64 bit integers
    Last edited by CommonTater; 05-14-2011 at 01:48 PM.

  9. #24
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    I will try to make the exercise and post the code here for checking.
    Both thanks for the help and patience.

    Roelof

  10. #25
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello,

    I need your help one more time.
    I now have this code:
    Code:
    /*
     * =====================================================================================
     *
     *       Filename:  test2.c
     *
     *    Description:  conventer from dec to 0101
     *
     *        Version:  1.0
     *        Created:  14-05-11 10:03:25
     *       Revision:  none
     *       Compiler:  gcc
     *
     *         Author:  Dr. Fritz Mehner (mn), mehner@fh-swf.de
     *        Company:  FH Südwestfalen, Iserlohn
     *
     * =====================================================================================
     */
    
    #include        <stdio.h>
    #include        <stdlib.h>
            int
    main ( int argc, char *argv[] )
    {
            int nummer, uitkomst, teller, remainder ;
            char uitkomst2[10] ;
            nummer = 4 ;
            uitkomst = 4 ;
            while (uitkomst <0)
            {
                    uitkomst = nummer/2;
                    printf ("uitkomst : %d", uitkomst);
                    remainder = nummer%2;
                    uitkomst2[teller]= remainder;
                    teller = teller +1 ;
                    nummer = uitkomst;
                    printf ("teller:%d  nummer:%d",teller, nummer);
            }
            while (teller !=0)
            {
                    printf("%d", uitkomst2[teller]);
                    teller = teller -1 ;
            }
    
            return EXIT_SUCCESS;
    }                               /* ----------  end of function main  ---------- */
    I now get no output except a cryptic command terminated message.

    Anyone see what I do wrong here ?

    Roelof

  11. #26
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    It's making me a little dizzy the way you're flipping numbers around in there...
    For example...
    Code:
     remainder = nummer%2;
     uitkomst2[teller]= remainder;
     teller = teller +1 ;
    Can be combined as...

    Code:
    uitkomst2[teller++]= nummer%2;
    with exactly the same result.

    It's always a good idea to go through and minmize your code, eliminating any unnecessary variables and combining statements when you can.

  12. #27
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    I changed the code to this :
    Code:
    /*
     *  * =====================================================================================
     *   
     *          Filename:  test2.c
     *    
     *          Description:  conventer from dec to 0101
     *       
     *          Version:  1.0
     *          Created:  14-05-11 10:03:25
     *          Revision:  none
     *          Compiler:  gcc
     *            
     *          Author:  Dr. Fritz Mehner (mn), mehner@fh-swf.de
     *          Company:  FH Südwestfalen, Iserlohn
     *               
     * =====================================================================================
      */
    
    #include        <stdio.h>
    #include        <stdlib.h>
    
    int
    main ( int argc, char *argv[] )
            {
                int nummer, uitkomst, teller ;
                char uitkomst2[10];
                nummer = 4 ;
                uitkomst = 4 ;
                while (uitkomst <1)
                    {
                            uitkomst = nummer/2;
                            uitkomst2[teller++]= nummer%2;
                            printf ("teller:%d  nummer:%d",teller, nummer);
                    }
                    while (teller !=0)
                      {
                            printf("%d", uitkomst2[teller]);
                            teller = teller -1 ;
                    }
       return EXIT_SUCCESS;
            }                              /* ----------  end of function main  ---------- */
    But still no output and a cryptic message command terminated.

    Anyone a idea waht happens.

    Roelof

  13. #28
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    I found it.
    I forget to set teller to 0

    But the problem of no output is still here

    Anyone who has tips for that.

    Roelof

  14. #29
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Three guesses as to how many times this loop runs:
    Code:
                uitkomst = 4 ;
                while (uitkomst <1)
                    {
                            uitkomst = nummer/2;
                            uitkomst2[teller++]= nummer%2;
                            printf ("teller:%d  nummer:%d",teller, nummer);
                    }

  15. #30
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by roelof View Post
    Oke,

    I found it.
    I forget to set teller to 0

    But the problem of no output is still here

    Anyone who has tips for that.

    Roelof
    Ok... look at the values of each variable as you come into the while loops...

    nummer = 4, uitkomst = 4, teller = 0;
    then you enter the loop... guarded by while (uitkomst less than 1)
    uitkomst = 4
    the loop never executes.
    Now it falls through to the second loop... while (teller != 0)
    Teller was unchanged by the first loop so teller = 0;
    The second loop never executes.

    Hense... no output.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Decimal to Binary conversion help
    By tru.cutru in forum C Programming
    Replies: 3
    Last Post: 07-08-2008, 11:17 PM
  2. binary decimal conversion
    By eerok in forum C Programming
    Replies: 2
    Last Post: 01-24-2006, 09:51 PM
  3. Decimal to binary conversion
    By blckspder in forum C Programming
    Replies: 5
    Last Post: 04-07-2005, 01:38 PM
  4. decimal to binary conversion
    By noob2c in forum C Programming
    Replies: 4
    Last Post: 05-29-2003, 09:07 PM
  5. Binary to decimal conversion help!
    By matrism in forum C Programming
    Replies: 4
    Last Post: 03-25-2002, 12:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21