33std::vector<std::bitset<32>> gray_code_generation(
int n) {
34 std::vector<std::bitset<32>>
gray_code = {};
41 int total_codes = 1 << n;
43 for (
int i = 0; i < total_codes; i++) {
44 int gray_num = i ^ (i >> 1);
45 gray_code.push_back(std::bitset<32>(gray_num));
59 std::vector<std::bitset<32>> gray_code_negative_1 = {};
61 std::vector<std::bitset<32>> gray_code_0 = {};
63 std::vector<std::bitset<32>> gray_code_1 = {
64 std::bitset<32>(0), std::bitset<32>(1)
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)
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)
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)
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)
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);
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);
Generate n-bit Gray code.