Skip to content

Commit

Permalink
Gray Code generation of any input bit length (#2005)
Browse files Browse the repository at this point in the history
  • Loading branch information
SubhradeepSS authored May 1, 2020
1 parent 3588067 commit 1457fe4
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions Gray_Code/Gray_Code.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//C++ program to generate the Gray Code for any input bit length

#include<bits/stdc++.h>
#define pb push_back
using namespace std;

// Function to generate the Gray Code sequence
void Gray(int n){
//If input size is less than or equal to 0
if(n <= 0){
cout << "Invalid input";
return;
}

vector<string> v;
v.pb("0");
v.pb("1");

for(int i = 2; i < (1 << n); i <<= 1){
for(int j = i - 1; j >= 0; j--)
v.pb(v[j]);

for(int j = 0; j < i; j++)
v[j] = "0" + v[j];

for(int j = i; j < 2 * i; j++)
v[j] = "1" + v[j];
}

cout << "Gray Code for bit length = " << n << " : " << endl;

for(int i = 0; i < v.size(); i++)
cout << v[i] << endl;
}

int main(){
int n;
cout << "Enter the bit length for Gray Code generation : ";
cin >> n;
Gray(n);
}

/* Sample input-output
Enter the bit length for Gray Code generation : 3
Gray Code for bit length = 3 :
000
001
011
010
110
111
101
100 */

0 comments on commit 1457fe4

Please sign in to comment.