Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
gray_code_example.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
36#include <iostream>
37#include <iomanip>
38#include <bitset>
39
40#include <ah-comb.H>
41#include <print_rule.H>
42
43using namespace Aleph;
44
45namespace
46{
48 template <size_t N>
49 std::string to_bin(uint32_t val)
50 {
51 std::bitset<N> bits(val);
52 return bits.to_string();
53 }
54}
55
56int main()
57{
58 std::cout << "\n=== Gray Code Utilities Demonstration ===\n\n";
59
60 {
61 std::cout << "Scenario A: Binary to Gray and back\n";
62 print_rule();
63 std::cout << std::left << std::setw(10) << "Decimal"
64 << std::setw(12) << "Binary"
65 << std::setw(12) << "Gray"
66 << "Recovered Binary\n";
67 print_rule();
68
69 for (uint32_t i = 0; i < 16; ++i)
70 {
71 uint32_t g = bin_to_gray(i);
72 uint32_t b = gray_to_bin(g);
73
74 std::cout << std::left << std::setw(10) << i
75 << std::setw(12) << to_bin<4>(i)
76 << std::setw(12) << to_bin<4>(g)
77 << to_bin<4>(b) << (i == b ? " [OK]" : " [ERR]") << "\n";
78 }
79 print_rule();
80 std::cout << "\n";
81 }
82
83 {
84 std::cout << "Scenario B: n-bit Gray code sequence generation\n";
85 print_rule();
86 const size_t n = 3;
87 auto sequence = build_gray_code(n);
88
89 std::cout << "Generated " << n << "-bit Gray code sequence:\n";
90 for (size_t i = 0; i < sequence.size(); ++i)
91 {
92 std::cout << " Step " << std::setw(2) << i << ": "
93 << to_bin<3>(sequence[i]);
94 if (i > 0)
95 {
96 uint32_t diff = sequence[i] ^ sequence[i-1];
97 std::cout << " (diff bit: " << std::popcount(diff) << ")";
98 }
99 std::cout << "\n";
100 }
101
102 // Check cyclic property
104 std::cout << " Cyclic difference bit count: " << std::popcount(cyclic_diff) << "\n";
105
106 print_rule();
107 std::cout << "\n";
108 }
109
110 {
111 std::cout << "Scenario C: Large value conversion\n";
112 print_rule();
113 uint64_t val = 0xDEADBEEFCAFEBABELL;
114 uint64_t g = bin_to_gray(val);
115 uint64_t b = gray_to_bin(g);
116
117 std::cout << "Original : 0x" << std::hex << val << "\n";
118 std::cout << "Gray : 0x" << g << "\n";
119 std::cout << "Recovered: 0x" << b << "\n";
120 std::cout << std::dec;
121 if (val == b)
122 std::cout << "Successfully converted and recovered 64-bit value.\n";
123 else
124 std::cout << "Error in 64-bit conversion.\n";
125 print_rule();
126 }
127
128 std::cout << "\nDone.\n";
129 return 0;
130}
Combinatorics utilities: permutations, combinations and matrix transposition.
int main()
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
constexpr T gray_to_bin(T g) noexcept
Convert a Gray code number to its binary representation.
Definition ah-comb.H:1144
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
Array< uint32_t > build_gray_code(const size_t n)
Generate the sequence of n-bit Gray codes.
Definition ah-comb.H:1164
bool diff(const C1 &c1, const C2 &c2, Eq e=Eq())
Check if two containers differ.
void print_rule()
Prints a horizontal rule for example output separation.
Definition print_rule.H:39
constexpr T bin_to_gray(const T n) noexcept
Convert a binary number to its Gray code representation.
Definition ah-comb.H:1126