### print all 64-bit binary numbers

I have a program in c++ to print all the binary numbers that have 64 bits. But the problem is it works only for 30 bits. Beyond that the program does not work possibly because of insufficient space availability. Any suggestions will be highly appreciated.

my code is as below:

// C++ program to generate n-bit binary numbers
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;

// This function generates all n bit Gray codes and prints the
// generated codes
void generateSequence(int n)
{
// base case
if (n <= 0)
return;
// 'arr' will store all generated codes
vector<string> arr;

arr.push_back("0");
arr.push_back("1");

// Every iteration of this loop generates 2*i codes from previously
// generated i codes.
int i, j;
for (i = 2; i < (1<<n); i = i<<1)
{
// Enter the prviously generated codes again in arr[].
// Now arr[] has double number of codes.
for (j = 0 ; j < i ; j++)
arr.push_back(arr[j]);

// append 0 to the first half
for (j = 0 ; j < i ; j++)
arr[j] = "0" + arr[j];

// append 1 to the second half
for (j = i ; j < 2*i ; j++)
arr[j] = "1" + arr[j];
}

// print contents of arr[]
for (i = 0 ; i < arr.size() ; i++ )
cout << arr[i] << endl;
}

// Driver program to test above function
int main()
{
int n;
printf("Enter the number : ");
scanf("%d", &n);
generateSequence(n);
return 0;
}
If you generate the combinations in order, you only ever need access to the combination you just completed to generate the next combination. Therefore, you should output combinations as you generate them and then discard them. There is no reason to store all of them.

If you will only ever need 64 bits, you might consider using a std::uint64_t to hold the bits, assuming your system has one, although a single std::string would be a more robust solution.
Thanks a lot. That was indeed a nice idea :)
Topic archived. No new replies allowed.