Convert Integer to Binary

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Darryl
    New Member
    • May 2007
    • 86

    #16
    Originally posted by ravenspoint
    Problem in line #9 - the "," should be a ";" I think.
    No, that's right, I think C uses the comma operator too.

    Originally posted by ravenspoint
    It is not good to cram so much code into one line, The compiler can keep it all straight, but it hurts my head to look at it!
    Yea, but I hate vertical space, I think I read once, debugging is easier and code is less likely to have errors if you can view it all at once without scrolling.

    Comment

    • ilikepython
      Recognized Expert Contributor
      • Feb 2007
      • 844

      #17
      Hey, I found another way:
      [code=c]
      char buffer[33];
      int num = 30;
      itoa(num, buffer, 2);
      cout << buffer << endl;
      [/code]

      Comment

      • ravenspoint
        New Member
        • Jul 2007
        • 111

        #18
        Originally posted by ilikepython
        Hey, I found another way:
        [code=c]
        char buffer[33];
        int num = 30;
        itoa(num, buffer, 2);
        cout << buffer << endl;
        [/code]
        Bravissimo!

        Today's lesson: RTFM

        Parameters

        value
        Number to be converted

        string
        String result

        radix
        Base of value; must be in the range 2 – 36

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #19
          Originally posted by Darryl
          Yea, but I hate vertical space, I think I read once, debugging is easier and code is less likely to have errors if you can view it all at once without scrolling.
          This is true, however it does not mean cram all the code onto one line otherwise by that argument your code is most maintainable in this form

          [code=c]
          #include "stdio.h"
          int main(){ char bits[33]="0000000000000 000000000000000 0000"; int index=0; unsigned number = 23, copy = number; while(copy>0) bits[31-index++]='0'+ (1 & copy), copy>>=1; printf("%d -> %s\n",number , bits);}
          [/code]

          What that statement means is that you should keep the individual code units (functions) small enough that the fit on your screen without the need to scroll, not that you should compress your code by using shortcuts and leaving out vertical space. The first will increase maintainability and reduce the chance of putting bugs in in the first place the second will result in less maintainable harder to read code.

          A good rule of thumb is "<= 1 statement per line"

          In general the comma operator is not a particularly useful one and normally only sees usage in the 1st and 3rd expressions of a for loop.

          Comment

          • joeschnell
            New Member
            • Apr 2007
            • 47

            #20
            Python, what does itoa mean in your code?

            Comment

            • ilikepython
              Recognized Expert Contributor
              • Feb 2007
              • 844

              #21
              Originally posted by joeschnell
              Python, what does itoa mean in your code?
              The function means to turn an integer into a string. The name just means integer to ascii (string) (i to a).

              Comment

              • umeshpatil
                New Member
                • Aug 2007
                • 2

                #22
                [HTML]
                Code:
                #include<stdio.h>
                #include<math.h>
                
                int main(int argc, char *argv[]){
                   int num=atoi(argv[1]),r,bin[10]; // takes number from commandline argument 
                  for(i=0;i<10;i++)
                          bin[i]=3;   
                 i=0; 
                 while(num!=0){
                       r=num%2;
                       bin[i]=r;
                       i++: 
                      num=num/2; 
                         
                       }
                 printf("\nNumber %d in binary is..\n");
                 for(i=9;i>=0;i--)
                    if(bin[i]!=3) printf("%d",bin[i]); 
                 getch(); 
                 return 0;
                }

                This codes shows converting number from decimal to binary.
                [/HTML]

                Comment

                • umeshpatil
                  New Member
                  • Aug 2007
                  • 2

                  #23
                  Code:
                  #include<stdio.h>
                  #include<math.h>
                  
                  int main(int argc, char *argv[]){
                     int num=atoi(argv[1]),r,bin[10]; // takes number from commandline argument 
                    for(i=0;i<10;i++)
                            bin[i]=3;   
                   i=0; 
                   while(num!=0){
                         r=num%2;
                         bin[i]=r;
                         i++: 
                        num=num/2; 
                           
                         }
                   printf("\nNumber %d in binary is..\n");
                   for(i=9;i>=0;i--)
                      if(bin[i]!=3) printf("%d",bin[i]); 
                   getch(); 
                   return 0;
                  }
                  This codes shows converting number from decimal to binary.

                  Comment

                  • LXSoft
                    New Member
                    • Jul 2012
                    • 1

                    #24
                    Without modulus operator: (works with any range of values)

                    Code:
                    int ttobit(int num)
                    {
                        int power = log2(num);
                        int res[99];
                        int sum = 0;
                        int counter = 0;
                    
                        while(power >= 0)
                        {
                            sum = sum + pow(2,power);
                    
                            if(num>=sum) {res[counter] = 1;}
                            else
                            {
                                sum = sum - pow(2,power);
                                res[counter] = 0;
                            }
                            counter++;
                            power--;
                        }
                    
                        return res[99];
                    }
                    With integer representation of bytes: (only works with values under 1024)

                    Code:
                    int ntobit(int num)
                    {
                        int power = log2(num);
                        unsigned long int res = 0;
                        
                        int sum = 0;
                    
                        while(power >= 0)
                        {
                            sum = sum + pow(2,power);
                    
                            if(num>=sum) { res = (res*10) + 1; }
                            else
                            {
                                sum = sum - pow(2,power);
                                res = (res*10) + 0;
                            }
                    
                            power--;
                        }
                    
                        return res;
                    }

                    Comment

                    • bibiki
                      New Member
                      • Sep 2009
                      • 6

                      #25
                      int numToConvert;
                      String rez = "";

                      while(numToConv ert > 0)
                      {
                      rez = (numToConvert%2 ) + rez;
                      numToConvert = (int)(numToConv ert/2);
                      }
                      //turn rez into an int, and there you go

                      Comment

                      • whodgson
                        Contributor
                        • Jan 2007
                        • 542

                        #26
                        On a minor point (@emaghero)the C++ pow() function return a double from its two double arguments

                        Comment

                        Working...