Conversion of decimal to binary in C programming n store in arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Zenoba
    New Member
    • Oct 2008
    • 8

    Conversion of decimal to binary in C programming n store in arrays

    Can someone give me an idea to convert decimal numbers into its binary equivalent and store each binary in arrays in C programming language??
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    In C, all the number types are, somewhere deep in your computer, actually stored as a row of binary bits, so I wouldn't call a number "decimal" unless it's decimal digits were stored in a string. But aside from that, to get a bit at a specific position in an int, you can make a int that has only a bit in the place in question, compare it to the original int with the bitwise and operator (so that the result has all false bits unless both the ints have a bit in the same position), and then cast the result to a bool. Hope this helps.
    Last edited by boxfish; Oct 1 '08, 03:07 PM. Reason: Said C++ when we were talking about C.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      What ideas do YOU have for it yet?

      Comment

      • Zenoba
        New Member
        • Oct 2008
        • 8

        #4
        Originally posted by r035198x
        What ideas do YOU have for it yet?
        i have idea for only one decimal number
        that is input a number then the program gives you its binary equivalent
        the code is as follows:

        Code:
        int num,no_bits;
        
        printf("Enter number");
        scanf("%d",&num);
        no_bits=count_bits(num);
        printf("number of bits %d",no_bits);
        bin_print(num,no_bits);
        
        count_bits(int n) //function to count number of bits 
        {
        int i=0;
        for (;n!=0;n=n>>1)
        i++;
        return(i);
        }
        bin_print(int x, int n_bits)
        /* function to print decimal numbers in binary format */
        {
        int j;
        printf("no. %d in binary \t",x);
        for(j=n_bits-1; j>=0;j--)
        printf("%i",(x>>j) & 01);
        }
        but that's only for one number
        i have to do it for a series of numbers stored in an array and store the binary ones in another array

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          There is a simple algorithm for this.

          Given the number 22 which has these bits:

          16 8 .. 4 .. 2 .. 1
          1.. 0 .. 1 .. 1 .. 0

          To see if the bit in the 4 position is set, divide the number by 4 and take the result %2. That is:

          22/4 = 5 and 5%2 is 1 --> the 4 bit is set
          22/8 = 2 and 2%2 is 0 --> the 8 bit is not set

          No need for fancy bit shifting.

          Comment

          • whodgson
            Contributor
            • Jan 2007
            • 542

            #6
            My book C++ In Plain English by Brian Overland has an interesting function for this:
            Code:
            void print_binary(short input_field)
            {
                int i,bit_set;
                
                while (i<=16)
                { bit_set = ((Ox8000 & input_field) >0);
                  printf("%d",bit_set);
                  input_field = input_field  << 1;
                  i++;}
            }
            I presume the equivalent can be written in C

            Comment

            • whodgson
              Contributor
              • Jan 2007
              • 542

              #7
              This is a function which emulates weaknessforcats ' algorithm
              Code:
              int convert(int n)
              {int k=1;
               while(k<n)//find the most significant bit
                k*=2;
               if(k>n)//fix the overshoot
                k=k/2;
              
               while(k>0)
               {
               if(int(n/k)%2==0)cout<<"0 ";
               else cout<<"1 ";
               k=k/2;
               }
              }

              Comment

              Working...