Gray code something--Recursive non-recursive implementation

Source: Internet
Author: User

Brief introduction

In the code of a group of numbers, if any two adjacent code is different from only one binary number, it is called the gray Code , and also because the maximum number and the minimum number are only one digit difference , that is, "end-to-end connection", Therefore, it is also called cyclic code or reflection code . In a digital system, code is often required to change in a certain order. For example, by increasing the number of natural numbers, if the number of 8421 yards, 0111 to 1000 when the four-bit changes, and in the actual circuit, 4-bit changes can not happen absolutely simultaneously, the count may appear in the short-term other code (1100, 1111, etc.). In certain cases, the circuit status error or input error may be caused. This error can be avoided by using gray code. Gray code is available in a variety of coding formats.

Gray code has used grey code, Grimes Code, code, Golay code, cyclic code, reflective binary code, the smallest error code and other names, some of them are wrong, some easily confused with other names, it is recommended not to use these used names.

Generate Gray Code

Gray code is a collection of numbers, each of which is represented by a binary, assuming that an n-bit is used to represent each number, and that only one bit value is different between any two numbers.  For example, the following is a 3-bit gray code: 000 001 011 010 110 111 101 100. If you want to produce N-bit gray codeSo The number of gray codes is 2^n . Assuming the original value starts at 0, Gray codethe law of productionis: The first step, change the rightmost bit value; The second step is to change the left bit of the first 1 bit; the third step, the fourth step, repeats the first and second steps until all the gray codes have been generated (in other words, it has gone (2^n)-1 steps). With a ExampleTo illustrate: Assuming a 3-bit gray code, the original value bit 000 The first step: Change the rightmost bit value: 001 Second step: Change the left bit of the first bit to 1:011 Step three: Change the rightmost bit value: 010 Fourth: Change the left bit of the first bit of 1 to the right: 110 Fifth step: Change the rightmost bit value: 111 Sixth step: Change the left bit of the first 1 bit: 101 Seventh Step: Change the rightmost bit value: 100 If you follow this rule to generate the gray code, there is no problem, but it is too complicated. If you look closely at the structure of the Gray code, we will have the following findings: 1. Except for the highest bit (the first bit on the left), the bit of gray code is completely up and down (see list below).  For example, the first gray code and the last Gray code symmetry (except the first one), the second Gray code and the penultimate symmetry, and so on. 2. the smallest repeating unit is 0, 1
0 xx
0
0
0 Ten
1 Ten
1
1
1 xx
So, when implemented, we can fully use recursion, add 0 or 1 in front of each layer, and then we can list all the gray codes.  For example: The first step: Generate 0, 12 strings. The second step: on the basis of the first step, each string is added 0 and 1, but can only add one at a time, so you have to do two times.  This becomes the 00,01,11,10 (attention symmetry).  Step three: On the basis of the second step, each string is added 0 and 1, again, can only add one at a time, so it becomes 000,001,011,010,110,111,101,100.  OK, so we can generate 3-bit gray code. To generate a 4-bit gray code, we just need to add another layer of 0,1 on the 3-bit gray code: 0000,0001,0011,0010,0110,0111,0101,0100,1100,1101,1110,1010,0111,1001,1000. In other words, The N-bit gray code is generated based on the n-1 bit-coded gray code.

Algorithm implementation

1. Recursive implementation

/*** Recursive generation of binary Gray code * Ideas: 1, get n-1 bit to generate Gray code array * 2, because n-bit generated gray code is twice times the number of n-1, so long as the n is the first half of the Gray code plus 0, the second half plus 1 can. * @paramnumber of bits in N Gray code *@returngenerated Gray code array*/     Public StaticString[] Graycode (intN) {//the size of the array is 2 of the N-square, because the N-bit gray code has 2 of the n-th orderstring[] Graycodearr =Newstring[(int) Math.pow (2, N)]; if(N < 1) {System.out.println ("The number of gray codes you have entered is incorrect!" "); }        if(1 = =N) {graycodearr[0] = "0"; graycodearr[1] = "1"; returnGraycodearr; }        //generation method of N-1 bit gray codeString[] before = Graycode (n-1);  for(inti = 0; i < before.length; i++) {Graycodearr[i]= "0" +Before[i]; Graycodearr[graycodearr.length-1-i] = "1" +Before[i]; }        returnGraycodearr; }

2, non-recursive implementation

  /*** Non-recursive generation of binary Gray code * Ideas: 1, get n-1 bit to generate Gray code array * 2, because n-bit generated gray code is twice times the number of n-1, so long as the n is the first half of the Gray code plus 0, the second half plus 1. * @paramnumber of bits in N Gray code *@returngenerated Gray code array*/     Public StaticString[] GrayCode2 (intN) {intnum = (int) Math.pow (2, N);//calculates the size of this gray sequence, based on the integer enteredString[] S1 = {"0", "1"};//First Gray sequence        if(N < 1) {System.out.println ("The number of gray codes you have entered is incorrect!" "); }         for(inti=2;i<=n;i++) {//Loop According to the first gray sequence, to one    intp = (int) Math.pow (2, I);//At the first few times, to calculate the size of this gray sequence .string[] Si =NewString[p];  for(intj=0;j<p;j++) {//The loop, based on a gray sequence, is one of the sequences .    if(j< (P/2) ) {Si[j]= "0" + s1[j];//Precede the original sequence with "0"}Else{Si[j]= "1" + s1[p-j-1];//original sequence reversed, preceded by "1"}} S1= SI;//The si to be obtained, attached to the S1, in order to find the next gray sequence    }        returnS1; }

3. Testing

 public  static  void   Main (string[] args) {System.out.println (" ———————————————————— recursion    Implement ———————————————— ");    string[] Strarr  = Graycode (4);  for  (int  i = 0; i < strarr.length ;    I++ "—————————————————— non-recursive implementation ————————————————"  = GrayCode2 (4);  for  (int  i = 0; i < Strarr2.length;    I++

4. Results:

———————————————————— Recursive implementation ———————————————— 0000000100110010011001110101010011001101111111101010101110011000—————————————————— Non-recursive implementation ———————————————— 0000000100110010011001110101010011001101111111101010101110011000

   Thanks: Thank you for your patience and reading!

Gray code something--Recursive non-recursive implementation

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.