Gray码 (格雷码) 【二进制】

以下内容是看了Matrix67的关于二进制的blog(Link)的一点总结与摘录。

Gray码,中文“格雷码”,是一种特殊的编码,相邻两个格雷码的二进制表示中有且仅有一位不同,且 n 阶 Gray 码是 0~2^n-1 的一个排列。

n 阶 Gray  码可以由 n-1 阶 Gray 码镜像翻转之后最前面加一个 ‘1‘ 得到。

比如 2 阶 Gray 码为:

00

01

11

10

3 阶:

000

001

011

010

110

111

101

100

这样就巧妙的实现了相邻的数只有一个二进制位不同。

Gray 与 Hanoi 塔问题有着密切联系,第 i 个 n 阶格雷码改变的是第 x 位,相应的 n 个圆盘的 Hanoi 塔在这第 i 步就移动编号为 x 的盘子。

这样,只要能计算出第 i 个 n 阶格雷码是什么,就能知道第 i 步的 n 阶 Hanoi 的移动步骤是什么。

求出第 i 个 Gray 码非常简单 : Gray(i) = i xor (i shr 1)           [C++] Gray[i] = i ^ (i >> 1);

这个证明可以看 Matrix67 的blog。

有关 Gray码的题目一道:Link

题目大意:给定 n 与 m,要求输出一个 2^n 行, 2^m 列的矩阵,里面填 0~2^(n+m)-1 的所有数字,每个数字与它相邻的数字的二进制表示都有且仅有一位不同。

题目分析:这道题是求一个“二维”的格雷码,然而我们有一个简便的构造方法,就是每个数字的前 n 位是 n 阶格雷码,后 m 位是 m 阶格雷码。

行与行之间的前 n 位不同,列与列之间的后 m 位不同,这样,由“一维”格雷码就轻松构造出“二维”格雷码了~

代码如下:

 

#include <cstdio>

using namespace std;

int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	int R, C, Num;
	R = 1 << n; C = 1 << m;
	for (int i = 0; i < R; ++i) {
		Num = (i ^ (i >> 1)) << m;
		for (int j = 0; j < C; ++j)
			printf("%d ", Num | (j ^ (j >> 1)));
		printf("\n");
	}
	return 0;
}

  

时间: 2025-01-18 10:07:17

Gray码 (格雷码) 【二进制】的相关文章

算法学习 - Gray Code(格雷码)的解释和c++实现

Gray Code(格雷码) 典型的二进制格雷码(Binary Gray Code)简称格雷码.当初是为了通信,现在则常用于模拟-数字转换和位置-数字转换中. 特点是:一组数的编码中,若任意两个相邻的代码只有一位二进制数不同,则称这种编码为格雷码. 格雷码属于可靠性编码,是一种错误最小化的编码方式. 格雷码是一种绝对编码方式. 由于格雷码是一种变权码. 格雷码的十进制数奇偶性与其码字中1的个数的奇偶性相同. 十进制转换为格雷码 好的上面我们已经介绍那么多了,那么我来说下如何把一个十进制的数字转换

LeetCode Gray Code 格雷码

题意:提供一个数字n,代表二进制的个数,那么就有2的n次方个可能性了,从0到2^n-1.将其转成格雷码,再直接将二进制的格雷码按二进制的读法变成整数,装在vector容器中返回,要有序(否则你直接将0-2^n-1返回算了). 思路:咋一看!什么是格雷码?假设有1个整数,是二进制形式的,将其最高位的1提出来,其他的每一位等于该位上的数字与该数字的前一位之异或. 好像很复杂的样子?举例: 整数 21 = 0001 0101 二进制 ,格雷码前4位是这样的0001,这就是将二进制的最高位的1提出来啦,

gray code 格雷码 递归

格雷码 the n-1 bit code, with 0 prepended to each word, followd by the n-1 bit code in reverse order, with 1 prepended to each word. public class GrayCode{ public static void gray(int n, String prefix) { if(n == 0) System.out.println(prefix); else { gra

[LeetCode] 89. Gray Code 格雷码

The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0. Ex

格雷码与二进制之间的互换

#include <stdio.h> #include <stdlib.h> /*格雷码转二进制*/ int grayToBin(int n) { int size = 0; int res[255]; int out = 0; int flag = 0; /*先缓存*/ while (n) { res[size++] = n%2; n /= 2; } /*利用转换关系求出最后的结果*/ for (int i=size-1; i>=0; i--) { flag = res[i

FPGA学习笔记之格雷码、边沿检测、门控时钟

一.格雷码 格雷码的优点主要是进位时只有一位跳变,误码率低. 1.二进制转格雷码 我们观察下表: 二进制码 格雷码 00 00 01 01 10 11 11 10 二进制码表示为B[],格雷码表示为G[],则有 G(i) = B(i),i为最高位 G(i-1) = B(i) xor B(i-1),i非最高位 用verilog可以这样写 1 reg [WIDTH-1:0] bin; 2 reg [WIDTH-1:0] gray; 3 parameter WIDTH = 8; 4 always @(

格雷码与二进制码的转换

Gray Code是1880年由法国工程师Jean-Maurice-Emlle Baudot发明的一种编码,是一种绝对编码方式,典型格雷码是一种具有反射特性和循环特性的单步自补码,它的循环.单步特性消除了随机取数时出现重大误差的可能,它的反射.自补特性使得求反非常方便.格雷码属于可靠性编码,是一种错误最小化的编码方式,因为,虽然自然二进制码可以直接由数/模转换器转换成模拟信号,但在某些情况,例如从十进制的3转换为4时二进制码的每一位都要变,能使数字电路产生很大的尖峰电流脉冲.而格雷码则没有这一缺

HDU HDU 5375 Gray code(二进制和格雷码)

Description: The reflected binary code, also known as Gray code after Frank Gray, is a binary numeral system where two successive values differ in only onebit (binary digit). The reflected binary code was originally designed to prevent spurious outpu

leetCode 89.Gray Code (格雷码) 解题思路和方法

The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0. Fo