Generate all lattice codes of n bits

Source: Internet
Author: User
This is similar to the implementation idea in the previous article. it does not use php bitwise operations and is less efficient. it fully reflects the conversion rules, basically: gray Code converts a Gray Code to a binary Code to a decimal Gray Code (Gray Code) is a collection of series. each number is represented by binary. assume that n-bit elements are used to represent each number, only one element value is different between any two numbers. For example, the Gray code of the following three yuan: 000 001 011 010 110 111 101 100. If you want to generate a Gray code of n bits, the number of Gray codes is 2 ^ n.
Assume that the original value starts from 0, and the Gray code generates the following rule: Step 1: Change the rightmost bitwise value; Step 2, change the left dollar of the first one from the right. Step 3, step 4 Repeat Step 1 and step 2 until all Gray codes are generated (in other words, (2 ^ n)-1 ).
An example is used to illustrate: assume that a Gray code with three digits is generated. The first step of the original value 000 is to change the rightmost bitwise value: 001 Step 2: change the Left bid of the first 1 bits from the right: 011 Step 3: Change the rightmost bits: 010 Step 4: change the Left bid of the first 1 bits from the right: 110 Step 5: change the rightmost bits value: 111 Step 6: change the Left bid of the first 1 bits from the right: 101 Step 7: change the rightmost bits: 100
If the Gray code is generated according to this rule, it is no problem, but this is too complicated. If we carefully observe the Gray code structure, we will find the following: 1. except for the highest bit (the first on the left), the Gray code's bitwise is fully symmetrical (see the list below ). For example, the first gray code is symmetric with the last Gray code (except the first one), the second Gray code is symmetric with the last one, and so on. 2. the smallest repeating unit is 0 and 1.
000
001
011
010
110
111
101
100

Therefore, we can use recursion to add 0 or 1 before each layer, and then we can list all gray codes. For example, Step 1: generate strings 0 and 1. Step 2: based on step 1, each string is added with 0 and 1, but only one can be added at a time, so you have to do it twice. In this way, it is changed to, (attention to symmetry ). Step 3: Add 0 and 1 to each string based on step 2. Similarly, you can add only one character string at a time, which is 000,001,011,010,110,111,101,100. Okay, so we can generate the three-digit Gray code. If you want to generate a 4-digit Gray code, you only need to add a layer of on the 3-digit Gray code, 1000.
That is to say, n-bit Gray code is generated based on n-1-bit Gray code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.