hi everyone....i have to convert decimal numbers starting from 1 to 1024 into binary and i need access to all the bits. i have done an example which generates a random number and converts it into a 10-bit binary number. this one worked fine for me...

for(i=0;i<10;i++)
	{
		sec[i]= rand()%1023;
		do
		{
			sym[i]= sec[i]%2;
			b=sec[i]/2;
			sec[i]=b;
			b=b<<1;
		}while(b=0);
                   printf(" %d", sym[i]);
	}

here the output is sym where i represents the 10 bits.


but now i need to convert all the numbers starting from 1 to 1024 like...

1 - 0000000001
2 - 0000000010
3 - 0000000011
.
.
.
.
.
1024 - 1000000000

Well here is a code in which you can generate numbers from 1 to 1023 i.e all numbers that can be represented in 10 bits.This is just one of the implementation and there are several other simpler ones too...You wanted access for all the bits so this is one way.You can make the changes needed and implement the same code for any higher number too...

#include<stdio.h>
int main()
{
     int i,j,num,sym[10]={0,0,0,0,0,0,0,0,0,0};
     for(i=1;i<1024;i++)
     {
                      num=i;
                      j=9;
                      while(num>0)
                      {
                                  sym[j--]=num%2;
                                  num=num/2;
                      }
                      printf("%d - ",i);
                      for(j=0;j<=9;j++)
                      {
                                       printf("%d",sym[j]);
                                       sym[j]=0;
                      }
                      printf("\n");
      }
      return 0;
}

oh...thanks a lot...its working!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.