this code is working for a counter of 6 bit and i need a counter of 48 bit ... if u have time kindly try it..... i try alot but not succeed

``````#include<stdio.h>
#include<stdlib.h>
#include<float.h>
#include<math.h>
#include <time.h>
#include <cstdlib>
#include <iostream>
#include<stdint.h>
#include<conio.h>

int main()
{

int a[64][6];
int  i,j,k,d,c,n;
for(k=0;k<64;k++) //6 digit binary so 2^6 =64 is no: of combinations
{
for(j=0;j<6;j++)
{
a[k][j]=0;
}
}
for(i=0;i<64;i++)
{
n=i;
for(j=5;j>=0;j--)
{
while(n!=0)
{
a[i][j]=n%2;
j--;
n=n/2;
}
}
}
for(k=0;k<64;k++)
{
for(j=0;j<6;j++)
{
printf("%d",a[k][j]);
}
printf(" .................%d\n",k);
}
//printf("\n **** KHATAM SHo ****\n");
getch();
return 0;

}

``````
2
Contributors
4
Replies
6
Views
5 Years
Discussion Span
Last Post by HunainHafeez

Such C++ includes in C code. The way you do it will become problematic when using 48 bits as the amount of possible permutations is huge. You don't have to store them though and just output them as they get calculated. Another thing that might become an issue is the use of counters (depending on how you do it). You could use an unsigned long long which is guaranteed to be 64 bits but it's a C99 standard feature. I'd go for re-writing the alghorithm though.

Depending on what your goal is exactly you could use a string. A quick attempt using a recursive method would result in something like this:

``````#include <stdio.h>

#define BIT_COUNT (6)

void print_bits(int bitcount, char* bitstring, int offset)
{
if (bitcount == 0)
{
printf("%s\n", bitstring);
}
else
{
bitstring[offset] = '0';
print_bits(bitcount - 1, bitstring, offset + 1);

bitstring[offset] = '1';
print_bits(bitcount - 1, bitstring, offset + 1);
}
}

int main()
{
char bits[BIT_COUNT + 1];
bits[BIT_COUNT] = '\0';

print_bits(BIT_COUNT, bits, 0);

return 0;
}
``````

@Gonbe, will your program exactly solve my problem ? ay i use it ?

It's more of a hint/possible solution towards what you need. As mentioned in the post it's something I quickly wrote down to show the idea of using a string you manipulate. It should work for an arbitrary bit count though, although I'm sure performance-wise it could be improved on.

You may use it, sure, but if you don't understand what's going on I don't see much of a point. I'll let you decide what you think is best for yourself.

-edit-

gonbe i tried to modify ur code for my problem but could't achieve, what i wanted

You should only have to change the define from 6 to 48. If that doesn't result in what you want I don't think I understand your problem correctly. If you set 48 it will take forever to finish, but that is to be expected with 2^48 strings that have to be calculated and printed. The logic of it should work. I'm not sure if I have time tomorrow to try to fix your existing code, it will probably be monday at the earliest if at any time. Try to figure out in the meanwhile what's going wrong and post your attempt(s). The bits increase, but that doesn't matter too much for the application's logic.

Edited by Gonbe