TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
gray_code.cpp
1
12#include <bitset>
13#include <cassert>
14#include <iostream>
15#include <vector>
16
21namespace bit_manipulation {
26namespace gray_code {
33std::vector<std::bitset<32>> gray_code_generation(int n) {
34 std::vector<std::bitset<32>> gray_code = {}; // Initialise empty vector
35
36 // No Gray codes for non-positive values of n
37 if (n <= 0) {
38 return gray_code;
39 }
40
41 int total_codes = 1 << n; // Number of n-bit gray codes
42
43 for (int i = 0; i < total_codes; i++) {
44 int gray_num = i ^ (i >> 1); // Gray code formula
45 gray_code.push_back(std::bitset<32>(gray_num)); // Store the value
46 }
47
48 return gray_code;
49}
50} // namespace gray_code
51} // namespace bit_manipulation
52
58static void test() {
59 std::vector<std::bitset<32>> gray_code_negative_1 = {};
60
61 std::vector<std::bitset<32>> gray_code_0 = {};
62
63 std::vector<std::bitset<32>> gray_code_1 = {
64 std::bitset<32>(0), std::bitset<32>(1)
65 };
66
67 std::vector<std::bitset<32>> gray_code_2 = {
68 std::bitset<32>(0), std::bitset<32>(1), std::bitset<32>(3), std::bitset<32>(2)
69 };
70
71 std::vector<std::bitset<32>> gray_code_3 = {
72 std::bitset<32>(0), std::bitset<32>(1), std::bitset<32>(3), std::bitset<32>(2),
73 std::bitset<32>(6), std::bitset<32>(7), std::bitset<32>(5), std::bitset<32>(4)
74 };
75
76 std::vector<std::bitset<32>> gray_code_4 = {
77 std::bitset<32>(0), std::bitset<32>(1), std::bitset<32>(3), std::bitset<32>(2),
78 std::bitset<32>(6), std::bitset<32>(7), std::bitset<32>(5), std::bitset<32>(4),
79 std::bitset<32>(12), std::bitset<32>(13), std::bitset<32>(15), std::bitset<32>(14),
80 std::bitset<32>(10), std::bitset<32>(11), std::bitset<32>(9), std::bitset<32>(8)
81 };
82
83 std::vector<std::bitset<32>> gray_code_5 = {
84 std::bitset<32>(0), std::bitset<32>(1), std::bitset<32>(3), std::bitset<32>(2),
85 std::bitset<32>(6), std::bitset<32>(7), std::bitset<32>(5), std::bitset<32>(4),
86 std::bitset<32>(12), std::bitset<32>(13), std::bitset<32>(15), std::bitset<32>(14),
87 std::bitset<32>(10), std::bitset<32>(11), std::bitset<32>(9), std::bitset<32>(8),
88 std::bitset<32>(24), std::bitset<32>(25), std::bitset<32>(27), std::bitset<32>(26),
89 std::bitset<32>(30), std::bitset<32>(31), std::bitset<32>(29), std::bitset<32>(28),
90 std::bitset<32>(20), std::bitset<32>(21), std::bitset<32>(23), std::bitset<32>(22),
91 std::bitset<32>(18), std::bitset<32>(19), std::bitset<32>(17), std::bitset<32>(16)
92 };
93
94 // invalid values for n
95 assert(bit_manipulation::gray_code::gray_code_generation(-1) == gray_code_negative_1);
96 assert(bit_manipulation::gray_code::gray_code_generation(0) == gray_code_0);
97
98 // valid values for n
99 assert(bit_manipulation::gray_code::gray_code_generation(1) == gray_code_1);
100 assert(bit_manipulation::gray_code::gray_code_generation(2) == gray_code_2);
101 assert(bit_manipulation::gray_code::gray_code_generation(3) == gray_code_3);
102 assert(bit_manipulation::gray_code::gray_code_generation(4) == gray_code_4);
103 assert(bit_manipulation::gray_code::gray_code_generation(5) == gray_code_5);
104}
105
110int main() {
111 test(); //Run self-test implementation
112 return 0;
113}
int main()
Main function.
Generate n-bit Gray code.