The realization of gray code

Source: Internet
Author: User

problem: Generate all gray codes for n-bits.  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 generate the gray code of N-bit, then the number of gray code is 2^n. assuming the original value starts at 0, gray code generated by the law is: the first step, change the rightmost bit value, the second step, change the right up to the first 1 bits of the left 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).  Use an example to illustrate:assuming that the 3-bit gray code is generated, the original value isFirst step: Change the rightmost bit value: 001Step Two: Change the left-hand side of the first 1 bit: 011Step Three: Change the rightmost bit value: 010Fourth Step: Change the left-hand side of the first 1 bit:Fifth Step: Change the rightmost bit value: 111Sixth step: Change the left bit of the first bit of the 1 to the right: 101Seventh Step: Change the rightmost bit value: If you follow this rule to generate 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, in addition to the highest bit (the first on the left), gray code bit completely up and down symmetry (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 repetition unit is 0, 1.   the
001
011
010
the
111
101
-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:Step One: 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 based on the n-1-bit gray code generation.  If you can understand the above section, the code implementation in the following section is easy to understand.
 PublicString[] Graycode (intN) {//produce 2^n grade codesstring[] Graycode =Newstring[(int) Math.pow (2, N)]; if(n = =1) {graycode[0] ="0"; graycode[1] ="1"; returnGraycode; } string[] last= Graycode (N-1);  for(inti =0; i < last.length; i++) {Graycode[i]="0"+Last[i]; Graycode[graycode.length-1-I] ="1"+Last[i]; }    returnGraycode;}
Gray Code also has a way to implement the formula is based on G (n) = B (n) XOR B (n+1), which is the conversion formula of gray code and binary code. the code is as follows:
 Public voidGetgraycode (intbitnum) {     for(inti =0; I < (int) Math.pow (2, Bitnum); i++){        intGraycode = (i >>1) ^i; System. out. println (Num2binary (Graycode, bitnum)); }} PublicString Num2binary (intNumintbitnum) {String ret="";  for(inti = bitnum-1; I >=0; i--) {ret+ = (num >> i) &1; }    returnret;}
transferred from: Blog.csdn.net/beiyeqingteng

The realization of 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.